欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

java.util.concurrent包的拆解

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

創(chuàng)新互聯(lián)建站專注于成都網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點(diǎn)出發(fā),讓客戶在網(wǎng)絡(luò)營(yíng)銷中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴(yán)謹(jǐn)?shù)膽B(tài)度對(duì)待客戶,用專業(yè)的服務(wù)創(chuàng)造價(jià)值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。

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

   /**
  • 在兩個(gè)獨(dú)立的線程中啟動(dòng)一個(gè)Producer和一個(gè)Consumer
  • Producer向一個(gè)共享的BlockingQueue注入字符串,而Comsumer從中拿出來(lái)
  • @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();//調(diào)用await()方法的線程會(huì)被掛起,會(huì)等待直到count值為0才繼續(xù)執(zhí)行
    System.out.println("2 end");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

/**

  • 所有線程都停留在柵欄的位置,都結(jié)束,才繼續(xù)執(zhí)行
  • @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可以控制同時(shí)訪問(wèn)的線程個(gè)數(shù),通過(guò)acquire()獲取一個(gè)許可,如果沒有就等待,而release()釋放一個(gè)許可。
  • @Author mufeng
  • @Date 2019-7-8 14:32
    */
    public class TestSemaphore {
    public static void main(String[] args) {
    int N=8;//工人數(shù)
    Semaphore semaphore=new Semaphore(5);//機(jī)器數(shù)
    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是在兩個(gè)任務(wù)之間交換對(duì)象的柵欄,當(dāng)這些任務(wù)進(jìn)入柵欄時(shí),它們各自擁有一個(gè)對(duì)象,當(dāng)它們離開時(shí),它們都擁有之前由對(duì)象持有的對(duì)象
  • @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創(chuàng)建一個(gè)可重用固定線程數(shù)的線程池,以共享的×××隊(duì)列方式來(lái)運(yùn)行線程。
2.newCachedThreadPool創(chuàng)建一個(gè)可緩存線程池,如果線程池長(zhǎng)度超過(guò)處理需要,可靈活回收空閑線程
3.newScheduledThreadPool創(chuàng)建一個(gè)定長(zhǎng)線程池,支持定時(shí)及周期性任務(wù)執(zhí)行
4.newSingleThreadExecutor創(chuàng)建一個(gè)單線程化的線程池,它只會(huì)用唯一的工作線程來(lái)執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO,LIFO,優(yōu)先級(jí))執(zhí)行

任務(wù)分兩類:一類是實(shí)現(xiàn)了Runnable接口的類,一類是實(shí)現(xiàn)了Callable接口的類, Callable的call()方法只能通過(guò)ExecutorService的submit(Callable task)方法來(lái)執(zhí)行,
并且返回一個(gè)Future.

4.lock
Synchronized缺點(diǎn)
1.無(wú)法中斷
2.無(wú)法設(shè)置超時(shí)
3.使用在方法上的synchronized其實(shí)是個(gè)語(yǔ)法糖

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

ReentrantLock 可重入鎖

5.atomic
標(biāo)量類:AtomicBoolean,AtomicInteger,AtomicLong,AtomicReference
數(shù)組類:AtomicIntegerArray,AtomicLongArray,AtomicReferenceArray
更新器類:AtomicLongFieldUpdater,AtomicIntegerFieldUpdater,AtomicReferenceFieldUpdater
復(fù)合變量類:AtomicMarkableReference,AtomicStampedReference

分享題目:java.util.concurrent包的拆解
文章出自:http://aaarwkj.com/article12/ipdgdc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、移動(dòng)網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷推廣、商城網(wǎng)站用戶體驗(yàn)、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)
国产一级黄色性生活片| 亚洲精品成人福利网站| 亚洲国产中文一区二区久久| 午夜福利尤物一区二区| 久久精品噜噜噜成人av农村| 亚洲精品综合在线二区| 国产亚洲综合另类色专区| 婷婷综合伊人久久狠狠| 亚洲欧美日韩不卡一区二区| 亚洲人成网站在线免费看| 国产粉嫩美女一区二区三| 国产精品一区二区黑人巨大| 成人免费毛片1000部| 精品一区二区亚洲精品| 午夜日韩综合激情视频在线观看| av中文字幕国产精品| 日本熟妇一区二区三区在线视频 | 午夜在线观看视频免费| 男人午夜福利视频在线观看| 亚洲熟女av综合网丁香| 视频在线免费观看97| 日本一区不卡二区高清| 欧美日韩一区二区高清在线| 国产亚洲理论片在线观看| 99在线视频午夜福利| 久亚洲精品色婷婷国产熟女| 天天躁人人躁夜夜躁狠狠躁| 日麻批视频在线免费观看| 在线日韩欧美一区二区三区| 欧美一区二区日韩国产| 麻豆午夜福利在线播放| 亚洲精品国产熟女av| 日韩暴露一区二区三区 | 97在线视频在线播放| 伊人激情一区二区三区| 精品妇女一区二区三区| 国产在线第一页第二页| 少妇高潮试看二十分钟| 亚洲精品熟女国产中文| 国产女人高潮流白丝视频| 中文字幕国产精品91|