91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java.util.concurrent包的拆解

發布時間:2020-07-23 15:59:04 來源:網絡 閱讀:298 作者:mufeng07 欄目:編程語言

java.util.concurrent包:
1.locks部分:顯式鎖(互斥鎖和速寫鎖)相關
2.atomic部分:原子變量類相關,是構建非阻塞算法的基礎
3.executor部分:線程池相關
4.collection部分:并發容器相關
5.tools部分:同步工具相關,如信號量、閉鎖、柵欄等功能

1.collection部分:
1.1 BlockingQueue
BlockingQueue為接口,如果要用它,需要實現它的子類:
ArrayBlockingQueue
DelayQueue
LinkedBlockingQueue
SynchronousQueue
PriorityBlockingQueue
TransferQueue

   /**
  • 在兩個獨立的線程中啟動一個Producer和一個Consumer
  • Producer向一個共享的BlockingQueue注入字符串,而Comsumer從中拿出來
  • @Author mufeng
  • @Date 2019-7-8 11:25
    */
    public class BlockingQueueExample {
    public static void main(String[] args) {
    BlockingQueue blockingQueue=new ArrayBlockingQueue(1024);
    Producer producer=new Producer(blockingQueue);
    Consumer consumer=new Consumer(blockingQueue);
    new Thread(producer).start();
    new Thread(consumer).start();
    try {
    Thread.sleep(4000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    class Producer implements Runnable{
    private BlockingQueue blockingQueue;
    public Producer(BlockingQueue blockingQueue){this.blockingQueue=blockingQueue;
    }
    @Override
    br/>this.blockingQueue=blockingQueue;
    }
    @Override
    try {
    blockingQueue.put("1");
    Thread.sleep(1000);
    blockingQueue.put("2");
    Thread.sleep(1000);
    blockingQueue.put("3");
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    class Consumer implements Runnable{
    private BlockingQueue blockingQueue;
    public Consumer(BlockingQueue blockingQueue){
    this.blockingQueue=blockingQueue;
    }

    @Override
    public void run() {
    try {
    System.out.println(blockingQueue.take());
    System.out.println(blockingQueue.take());
    System.out.println(blockingQueue.take());
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

2.Tools部分
2.1 CountDownLatch用法

/**

  • @Author mufeng
  • @Date 2019-7-8 11:54
    */
    public class TestCountDownLatch {
    public static void main(String[] args) {
    final CountDownLatch countDownLatch=new CountDownLatch(2);
    new Thread(){
    public void run(){
    System.out.println(Thread.currentThread().getName()+"processing");
    try {
    Thread.sleep(3000);
    System.out.println(Thread.currentThread().getName()+"ended");
    countDownLatch.countDown();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }.start();
    new Thread(){
    public void run(){
    System.out.println(Thread.currentThread().getName()+"processing");
    try {
    Thread.sleep(3000);
    System.out.println(Thread.currentThread().getName()+"ended");
    countDownLatch.countDown();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }.start();
    try {
    System.out.println("wait success...");
    countDownLatch.await();//調用await()方法的線程會被掛起,會等待直到count值為0才繼續執行
    System.out.println("2 end");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

/**

  • 所有線程都停留在柵欄的位置,都結束,才繼續執行
  • @Author mufeng
  • @Date 2019-7-8 14:10
    */
    public class TestCyclicBarrier {
    public static void main(String[] args) {
    int n=4;
    CyclicBarrier cyclicBarrier=new CyclicBarrier(n);
    for(int i=0;i<n;i++)
    new Writer(cyclicBarrier).start();
    }

}
class Writer extends Thread{
private CyclicBarrier cyclicBarrier;
public Writer(CyclicBarrier cyclicBarrier){
this.cyclicBarrier=cyclicBarrier;
}
public void run(){
try {
System.out.println("writer start");
Thread.sleep(5000);
System.out.println("writer end");
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("continue...");
}
}

/**

  • Semaphore可以控制同時訪問的線程個數,通過acquire()獲取一個許可,如果沒有就等待,而release()釋放一個許可。
  • @Author mufeng
  • @Date 2019-7-8 14:32
    */
    public class TestSemaphore {
    public static void main(String[] args) {
    int N=8;//工人數
    Semaphore semaphore=new Semaphore(5);//機器數
    for(int i=0;i<N;i++){
    new Worker(i,semaphore).start();
    }
    }
    }
    class Worker extends Thread{
    private int num;
    private Semaphore semaphore;
    public Worker(int num,Semaphore semaphore){
    this.num=num;
    this.semaphore=semaphore;
    }
    public void run(){
    try {
    semaphore.acquire();
    System.out.println("工人"+this.num+"occupy a machine。。。");
    Thread.sleep(2000);
    semaphore.release();
    System.out.println("工人"+this.num+"release a machine");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

/**

  • Exchanger是在兩個任務之間交換對象的柵欄,當這些任務進入柵欄時,它們各自擁有一個對象,當它們離開時,它們都擁有之前由對象持有的對象
  • @Author mufeng
  • @Date 2019-7-8 14:54
    */
    public class TestExchanger {
    public static void main(String[] args) {
    ExecutorService executor =Executors.newCachedThreadPool();
    final Exchanger exchanger=new Exchanger();
    executor.execute(new Runnable() {@Override
    br/>@Override
    String data1="li";
    doExchangeWork(data1,exchanger);
    }
    });
    executor.execute(new Runnable() {@Override
    br/>@Override
    String data1="zhang";
    doExchangeWork(data1,exchanger);
    }
    });
    executor.shutdown();
    }
    private static void doExchangeWork(String data1,Exchanger exchanger){
    System.out.println(Thread.currentThread().getName()+"exchange:"+data1);
    try {
    String data2 = (String) exchanger.exchange(data1);
    System.out.println(Thread.currentThread().getName()+"exchange to"+data2);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

3.Executor
四種線程池:newFixedThreadPool,newCachedThreadPool,newSingleThreadExecutor,newScheduledThreadPool
1.newFixedThreadPool創建一個可重用固定線程數的線程池,以共享的×××隊列方式來運行線程。
2.newCachedThreadPool創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程
3.newScheduledThreadPool創建一個定長線程池,支持定時及周期性任務執行
4.newSingleThreadExecutor創建一個單線程化的線程池,它只會用唯一的工作線程來執行任務,保證所有任務按照指定順序(FIFO,LIFO,優先級)執行

任務分兩類:一類是實現了Runnable接口的類,一類是實現了Callable接口的類, Callable的call()方法只能通過ExecutorService的submit(Callable task)方法來執行,
并且返回一個Future.

4.lock
Synchronized缺點
1.無法中斷
2.無法設置超時
3.使用在方法上的synchronized其實是個語法糖

lock(),trylock(),tryLock(long time,TimeUnit unit)和lockInterruptibly()是用來獲取鎖的,unlock()方法是用來釋放鎖的。

ReentrantLock 可重入鎖

5.atomic
標量類:AtomicBoolean,AtomicInteger,AtomicLong,AtomicReference
數組類:AtomicIntegerArray,AtomicLongArray,AtomicReferenceArray
更新器類:AtomicLongFieldUpdater,AtomicIntegerFieldUpdater,AtomicReferenceFieldUpdater
復合變量類:AtomicMarkableReference,AtomicStampedReference

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

县级市| 巫溪县| 安塞县| 西充县| 台北县| 增城市| 郁南县| 措美县| 祁东县| 阿坝县| 湘潭县| 道孚县| 嘉义县| 余江县| 罗源县| 桃园县| 平陆县| 岑巩县| 中西区| 诏安县| 石门县| 湖南省| 孟连| 板桥市| 闻喜县| 远安县| 宽城| 潞城市| 肃宁县| 芮城县| 宜兰县| 外汇| 辛集市| 秦皇岛市| 曲沃县| 通州区| 梁平县| 密山市| 昆明市| 和顺县| 颍上县|