涓€閬撳绾跨▼闈㈣瘯棰樼殑澶氱瑙e喅鏂规硶
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了涓€閬撳绾跨▼闈㈣瘯棰樼殑澶氱瑙e喅鏂规硶相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/cut' title='cut'>cut
寰幆 int 娑堣垂 rac 杩涘叆 tac bottom ++涓€閬撳绾跨▼闈㈣瘯棰樼殑澶氱瑙e喅鏂规硶
棰樼洰
瀹炵幇涓€涓鍣紝绾跨▼2娣诲姞10涓厓绱狅紝绾跨▼1瀹炴椂鐩戠潱绾跨▼2锛屽綋瀹瑰櫒涓厓绱犱釜鏁拌揪鍒?涓椂锛岀粰鍑烘彁绀哄苟缁撴潫銆?/p>
鍒嗘瀽
杩欓鏈韩骞朵笉鏄緢闅撅紝绫讳技鐢熶骇鑰呫€佹秷璐硅€呴棶棰橈紝绾跨▼1绛夊緟绾跨▼2娣诲姞鍏冪礌杈惧埌5涓紝绾跨▼1娑堣垂杩欎釜浜嬩欢銆?br> 鍥犳鍙互鎯冲埌鐨勬槸鍒╃敤鐢熶骇鑰呮秷璐硅€呰寖寮忔潵瑙e喅銆?br> 鎴戜富瑕佺敤鐢熶骇鑰呮秷璐硅€呰寖寮忓拰JUC涓悇涓父鐢ㄧ粍浠舵潵瑙e喅杩欎釜闂锛岀啛鎮変笅鍚勪釜缁勪欢浣跨敤鏂规硶銆?/p>
鏁翠綋娴嬭瘯璁捐
涓轰簡渚夸簬娴嬭瘯锛岃繖閲屽缓绔嬩簡涓€涓猅1_Supervice_T2鎶借薄绫伙紝鎻愪緵涓や釜妯℃澘鏂规硶锛屽瓙绫诲疄鐜板苟鐢ㄤ綔t1, t2 绾跨▼鐨勮繍琛屾柟娉曘€俆1_Supervice_T2_Test鐢ㄦ潵娴嬭瘯鍚勪釜绫荤殑鏂规硶锛屾暣浣撶粨鏋勫涓嬶細
T1_Supervice_T2鎶借薄鐖剁被濡備笅锛?/p>
/**
* t2涓坊鍔犲崄涓厓绱狅紝t1鐩戠潱t2锛宼2娣诲姞浜?涓厓绱犳椂缁欏嚭閫氱煡
*/
public abstract class T1_Supervice_T2 {
public List<Integer> list = new ArrayList<>();
public Thread t1;
public Thread t2;
public T1_Supervice_T2(){
t1 = new Thread(()->run1());
t2 = new Thread(()->run2());
}
public void execute(){
t1.start();
t2.start();
}
public abstract void run1();
public abstract void run2();
}
娴嬭瘯绫诲涓嬶細
public class T1_Supervice_T2_Test {
public static void main(String[] args) {
// T1_Supervice_T2 test = new T1_Supervice_T2_Synchronized();
// T1_Supervice_T2 test = new T1_Supervice_T2_ReentrantLock();
// T1_Supervice_T2 test = new T1_Supervice_T2_CountDownLatch();
// T1_Supervice_T2 test = new T1_Supervice_T2_CycliBarrier();
// T1_Supervice_T2 test = new T1_Supervice_T2_Semaphore();
T1_Supervice_T2 test = new T1_Supervice_T2_LockSupport();
test.execute();
}
}
鏂规硶
绗?銆?绉嶆柟娉曟槸鍒╃敤鐢熶骇鑰呮秷璐硅€呰寖寮忔潵鍐欙紝鑼冨紡濡備笅锛?/p>
绛夊緟鏂癸紙娑堣垂鑰咃級锛? 鑾峰緱瀵硅薄閿? 鏉′欢涓嶆弧瓒筹紝鍒欑瓑寰? 鏉′欢婊¤冻锛屽垯鎵ц
閫氱煡鏂癸紙鐢熶骇鑰咃級锛? 鑾峰緱瀵硅薄閿? 鏀瑰彉鏉′欢
閫氱煡鍦ㄧ瓑寰呭璞′笂鐨勭嚎绋?/code>
鍩轰簬Synchronized
娉ㄦ剰锛歸ait鍜宯otify蹇呴』瑕佺敤鍦╯ynchronized涓紝鍦ㄥ闈細鎶ラ敊
public class T1_Supervice_T2_Synchronized extends T1_Supervice_T2 {
private Object lock = new Object();
@Override
public void run1() {
synchronized (lock){
while (list.size() != 5){
try {
lock.wait();
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("---------------list has 5 elements!---------------");
lock.notify();
}
}
}
@Override
public void run2() {
synchronized (lock){
for (int i = 0; i < 10; i++) {
System.out.println("list adds " + (i+1) + "th element");
list.add(i);
if(list.size() == 5){
lock.notify();
try {
lock.wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
}
}
鍩轰簬ReentrantLock
娉ㄦ剰锛?/strong> await鏂规硶鍜宻ignal鏂规硶寰楀湪lock涓棿銆傚苟瑕佹樉绀虹殑瑙i攣 CountDownLatch鏄竴涓棬鏍擄紝鍒拌揪鏌愪釜涓€瀹氭暟閲忓悗锛屽氨鍏佽绾跨▼缁х画鎵ц涓嬪幓锛屼絾姣忔鍙兘璁剧疆涓€娆★紝鍥犳鍦╰2 绾跨▼涓湁涓€涓啀涓€娆$殑璧嬪€?br>
璋冪敤锛歛wait鍜宑ountDown鏂规硶 CycliBarrier鏄惊鐜爡鏍忥紝鍚屾椂鍒拌揪涓€瀹氭暟閲忓悗锛屽厑璁告墍鏈夌嚎绋嬬户缁墽琛岋紝鍚屾椂鎭㈠鍒拌缃殑鍒濆鍊笺€?br>
鍙渶瑕佽皟鐢╝wait鏂规硶鍗冲彲 Semaphore鏄俊鍙烽噺锛岀敤鏉ラ檺娴佺殑锛屽悓鏃跺厑璁竛 涓嚎绋嬭繍琛岋紝涓昏鏂规硶acquire鐢ㄦ潵璇锋眰鑾峰彇浣跨敤鏉冿紝涓?鏃堕樆濉炪€俽elease鏄噴鏀俱€?br>
杩欓噷瀹為檯涓婃病鏈夌敤鍒癝emaphore锛屼富瑕佹槸join鍚埌浜嗚皟鏁磋繍琛岄『搴忋€?/p>
LockSupport鏄疛UC涓竴涓敮鎸佹€у寘銆俻ark鍙互浣跨嚎绋嬭繘鍏aiting鐘舵€併€倁npark鍒欏搴旂嚎绋嬮€€鍑簑aiting鐘舵€併€傝繖涓笉闇€瑕佸湪閿佷笅浣跨敤锛屾瘮杈冪伒娲伙紝涓斾唬鐮佺畝娲併€?/p>
杩欓噷鐢ㄤ袱涓潯浠讹紝閫昏緫鏇村姞娓呮櫚涓€鐐?/p>
public class T1_Supervice_T2_ReentrantLock extends T1_Supervice_T2 {
private ReentrantLock lock = new ReentrantLock();
private Condition turn_t1 = lock.newCondition();
private Condition turn_t2 = lock.newCondition();
@Override
public void run1() {
try {
lock.lock();
while (list.size() != 5)
turn_t1.await();
System.out.println("---------------list has 5 elements!---------------");
turn_t2.signal();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
@Override
public void run2() {
try {
lock.lock();
for (int i = 0; i < 10; i++) {
System.out.println("list adds " + (i + 1) + "th element");
list.add(i);
if (list.size() == 5) {
turn_t1.signal();
turn_t2.await();
}
}
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
鍩轰簬CountDownLatch
public class T1_Supervice_T2_CountDownLatch extends T1_Supervice_T2 {
private CountDownLatch latch = new CountDownLatch(1);
@Override
public void run1() {
if(list.size() != 5){
try {
latch.await();
}catch (InterruptedException e){
e.printStackTrace();
}
}
System.out.println("---------------list has 5 elements!---------------");
latch.countDown();
}
@Override
public void run2() {
for (int i = 0; i < 10; i++) {
System.out.println("list adds " + (i+1) + "th element");
list.add(i);
if(list.size() == 5){
latch.countDown();
latch = new CountDownLatch(1);
try {
latch.await();
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
}
鍩轰簬CycliBarrier
public class T1_Supervice_T2_CycliBarrier extends T1_Supervice_T2{
CyclicBarrier barrier = new CyclicBarrier(2);
@Override
public void run1() {
if(list.size() != 5){
try {
barrier.await();
System.out.println("---------------list has 5 elements!---------------");
barrier.await();
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public void run2() {
for (int i = 0; i < 10; i++) {
System.out.println("list adds " + (i+1) + "th element");
list.add(i);
if(list.size() == 5){
try {
barrier.await();
barrier.await();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
鍩轰簬Semaphore
public class T1_Supervice_T2_Semaphore extends T1_Supervice_T2{
private Semaphore semaphore = new Semaphore(1);
@Override
public void execute(){
t2.start();
}
@Override
public void run1() {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("---------------list has 5 elements!---------------");
semaphore.release();
}
@Override
public void run2() {
try {
//semaphore.acquire();
for (int i = 0; i < 10; i++) {
System.out.println("list adds " + (i + 1) + "th element");
list.add(i);
if (list.size() == 5) {
semaphore.release();
t1.start();
t1.join();
semaphore.acquire();
}
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
鍩轰簬LockSupport
public class T1_Supervice_T2_LockSupport extends T1_Supervice_T2 {
@Override
public void run1() {
if(list.size() != 5){
LockSupport.park();
}
System.out.println("---------------list has 5 elements!---------------");
LockSupport.unpark(t2);
}
@Override
public void run2() {
for (int i = 0; i < 10; i++) {
System.out.println("list adds " + (i+1) + "th element");
list.add(i);
if(list.size() == 5){
LockSupport.unpark(t1);
LockSupport.park();
}
}
}
}
以上是关于涓€閬撳绾跨▼闈㈣瘯棰樼殑澶氱瑙e喅鏂规硶的主要内容,如果未能解决你的问题,请参考以下文章
Linux mysql蹇樿瀵嗙爜 淇敼瀵嗙爜 瑙e喅鏂规硶
浣跨敤IScroll缁勪欢鍑虹幇婊戝姩鍗¢闂瑙e喅鏂规硶
IIS HTTP 閿欒 404.17 - Not Found 瑙e喅鏂规硶