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),如信號量、閉鎖、柵欄等功能
創(chuàng)新互聯(lián)建站專注于成都網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點(diǎn)出發(fā),讓客戶在網(wǎng)絡(luò)營銷中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴(yán)謹(jǐn)?shù)膽B(tài)度對待客戶,用專業(yè)的服務(wù)創(chuàng)造價值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。
1.collection部分:
1.1 BlockingQueue
BlockingQueue為接口,如果要用它,需要實(shí)現(xiàn)它的子類:
ArrayBlockingQueue
DelayQueue
LinkedBlockingQueue
SynchronousQueue
PriorityBlockingQueue
TransferQueue
/**
@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用法
/**
/**
}
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...");
}
}
/**
/**
3.Executor
四種線程池:newFixedThreadPool,newCachedThreadPool,newSingleThreadExecutor,newScheduledThreadPool
1.newFixedThreadPool創(chuàng)建一個可重用固定線程數(shù)的線程池,以共享的×××隊(duì)列方式來運(yùn)行線程。
2.newCachedThreadPool創(chuàng)建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程
3.newScheduledThreadPool創(chuàng)建一個定長線程池,支持定時及周期性任務(wù)執(zhí)行
4.newSingleThreadExecutor創(chuàng)建一個單線程化的線程池,它只會用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO,LIFO,優(yōu)先級)執(zhí)行
任務(wù)分兩類:一類是實(shí)現(xiàn)了Runnable接口的類,一類是實(shí)現(xiàn)了Callable接口的類, Callable的call()方法只能通過ExecutorService的submit(Callable task)方法來執(zhí)行,
并且返回一個Future.
4.lock
Synchronized缺點(diǎn)
1.無法中斷
2.無法設(shè)置超時
3.使用在方法上的synchronized其實(shí)是個語法糖
lock(),trylock(),tryLock(long time,TimeUnit unit)和lockInterruptibly()是用來獲取鎖的,unlock()方法是用來釋放鎖的。
ReentrantLock 可重入鎖
5.atomic
標(biāo)量類:AtomicBoolean,AtomicInteger,AtomicLong,AtomicReference
數(shù)組類:AtomicIntegerArray,AtomicLongArray,AtomicReferenceArray
更新器類:AtomicLongFieldUpdater,AtomicIntegerFieldUpdater,AtomicReferenceFieldUpdater
復(fù)合變量類:AtomicMarkableReference,AtomicStampedReference