涓€閬撳绾跨▼闈㈣瘯棰樼殑澶氱瑙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鐢ㄦ潵娴嬭瘯鍚勪釜绫荤殑鏂规硶锛屾暣浣撶粨鏋勫涓嬶細
鎶€鏈浘鐗? src=

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攣
杩欓噷鐢ㄤ袱涓潯浠讹紝閫昏緫鏇村姞娓呮櫚涓€鐐?/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

CountDownLatch鏄竴涓棬鏍擄紝鍒拌揪鏌愪釜涓€瀹氭暟閲忓悗锛屽氨鍏佽绾跨▼缁х画鎵ц涓嬪幓锛屼絾姣忔鍙兘璁剧疆涓€娆★紝鍥犳鍦╰2 绾跨▼涓湁涓€涓啀涓€娆$殑璧嬪€?br> 璋冪敤锛歛wait鍜宑ountDown鏂规硶

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

CycliBarrier鏄惊鐜爡鏍忥紝鍚屾椂鍒拌揪涓€瀹氭暟閲忓悗锛屽厑璁告墍鏈夌嚎绋嬬户缁墽琛岋紝鍚屾椂鎭㈠鍒拌缃殑鍒濆鍊笺€?br> 鍙渶瑕佽皟鐢╝wait鏂规硶鍗冲彲

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

Semaphore鏄俊鍙烽噺锛岀敤鏉ラ檺娴佺殑锛屽悓鏃跺厑璁竛 涓嚎绋嬭繍琛岋紝涓昏鏂规硶acquire鐢ㄦ潵璇锋眰鑾峰彇浣跨敤鏉冿紝涓?鏃堕樆濉炪€俽elease鏄噴鏀俱€?br> 杩欓噷瀹為檯涓婃病鏈夌敤鍒癝emaphore锛屼富瑕佹槸join鍚埌浜嗚皟鏁磋繍琛岄『搴忋€?/p>

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

LockSupport鏄疛UC涓竴涓敮鎸佹€у寘銆俻ark鍙互浣跨嚎绋嬭繘鍏aiting鐘舵€併€倁npark鍒欏搴旂嚎绋嬮€€鍑簑aiting鐘舵€併€傝繖涓笉闇€瑕佸湪閿佷笅浣跨敤锛屾瘮杈冪伒娲伙紝涓斾唬鐮佺畝娲併€?/p>

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喅鏂规硶

鏈€杩戦亣鍒伴棶棰樼殑瑙e喅鏂规锛氱壒娈婄殑绌烘牸-ASCII鐮佸€?60锛堣浆鍙戯級

ESXi鍒涘缓datastore鎶ラ敊Fail to create VMFS datastore瑙e喅鏂规硶