class Tester{
private boolean flag = true ;//設(shè)置標(biāo)志位,初始時(shí)先生產(chǎn)
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition(); //產(chǎn)生一個(gè)Condition對(duì)象
publicvoid Method1(){
lock.lock();
try{
while(!flag){
condition.await() ;
}
//Do sth
flag= false ; //改變標(biāo)志位胎撤,表示可以取走
condition.signal();
}catch(InterruptedException e){
e.printStackTrace() ;
}finally{
lock.unlock();
}
}
public void Method2(){
lock.lock();
try{
while(flag){
condition.await() ;
}
//Do sth
flag= true ;//改變標(biāo)志位瓮钥,表示可以生產(chǎn)
condition.signal();
}catch(InterruptedException e){
e.printStackTrace() ;
}finally{
lock.unlock();
}
}
}
class Producer implements Runnable{
private Tester producerTester = null ;
public Producer(Tester producerTester){
this.producerTester = producerTester ;
}
public void run(){
boolean flag = true ;
for(int i=0;i<10;i++){
this.producerTester.Method1();
if(flag){
flag = false ;
}else{
flag = true ;
}
}
}
}
class Consumer implements Runnable{
private Tester consumerTester = null ;
public Consumer(Tester consumerTester){
this.consumerTester = consumerTester ;
}
public void run(){
for(int i=0;i<10;i++){
this.consumerTester.Method2() ;
}
}
}
public class ThreadCaseDemo{
public static void main(String args[]){
Tester tester = new Tester();
Producer pro = new Producer(tester) ;
Consumer con = new Consumer(tester) ;
new Thread(pro).start() ;
try{
Thread.sleep(500) ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
new Thread(con).start() ;
}
}
讀寫鎖:
通過synchronized獲取的互斥鎖不僅互斥讀寫操作、寫寫操作俊马,還互斥讀讀操作,而讀讀操作時(shí)不會(huì)帶來數(shù)據(jù)競(jìng)爭(zhēng)的,因此對(duì)對(duì)讀讀操作也互斥的話萝嘁,會(huì)降低性能。Java 5中提供了讀寫鎖扬卷,它將讀鎖和寫鎖分離牙言,使得讀讀操作不互斥。
[if !supportLists]1[endif]線程池使用
包:java.uitl.concurrent.ThreadPoolExecutor
[if !supportLists]1.1[endif]線程池類型
public class ThreadPoolExecutor extendsAbstractExecutorService {
.....
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,longkeepAliveTime,TimeUnit unit, BlockingQueue workQueue);
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,longkeepAliveTime,TimeUnit unit, BlockingQueueworkQueue,ThreadFactory threadFactory);
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnitunit, BlockingQueue workQueue,RejectedExecutionHandlerhandler);
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,longkeepAliveTime,TimeUnit unit, BlockingQueueworkQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler);
...
}
corePoolSize:核心池的大小怪得。在創(chuàng)建了線程池后咱枉,默認(rèn)情況下,線程池中并沒有任何線程徒恋,而是等待有任務(wù)到來才創(chuàng)建線程去執(zhí)行任務(wù)蚕断,除非調(diào)用了prestartAllCoreThreads()或者prestartCoreThread()方法,從這2個(gè)方法的名字就可以看出入挣,是預(yù)創(chuàng)建線程的意思亿乳,即在沒有任務(wù)到來之前就創(chuàng)建corePoolSize個(gè)線程或者一個(gè)線程。默認(rèn)情況下径筏,在創(chuàng)建了線程池后葛假,線程池中的線程數(shù)為0,當(dāng)有任務(wù)來之后滋恬,就會(huì)創(chuàng)建一個(gè)線程去執(zhí)行任務(wù)聊训,當(dāng)線程池中的線程數(shù)目達(dá)到corePoolSize后,就會(huì)把到達(dá)的任務(wù)放到緩存隊(duì)列當(dāng)中恢氯;
maximumPoolSize:線程池最大線程數(shù)带斑,表示在線程池中最多能創(chuàng)建多少個(gè)線程;
keepAliveTime:表示線程沒有任務(wù)執(zhí)行時(shí)最多保持多久時(shí)間會(huì)終止酿雪。默認(rèn)情況下遏暴,只有當(dāng)線程池中的線程數(shù)大于corePoolSize時(shí)侄刽,keepAliveTime才會(huì)起作用指黎,直到線程池中的線程數(shù)不大于corePoolSize,即當(dāng)線程池中的線程數(shù)大于corePoolSize時(shí)州丹,如果一個(gè)線程空閑的時(shí)間達(dá)到keepAliveTime醋安,則會(huì)終止,直到線程池中的線程數(shù)不超過corePoolSize墓毒。但是如果調(diào)用了allowCoreThreadTimeOut(boolean)方法吓揪,在線程池中的線程數(shù)不大于corePoolSize時(shí),keepAliveTime參數(shù)也會(huì)起作用所计,直到線程池中的線程數(shù)為0柠辞;
unit:參數(shù)keepAliveTime的時(shí)間單位,有7種取值主胧,在TimeUnit類中有7種靜態(tài)屬性:
[if !supportLists]l[endif]TimeUnit.DAYS;//天
[if !supportLists]l[endif]TimeUnit.HOURS;//小時(shí)
[if !supportLists]l[endif]TimeUnit.MINUTES;//分鐘
[if !supportLists]l[endif]TimeUnit.SECONDS;//秒
[if !supportLists]l[endif]TimeUnit.MILLISECONDS;//毫秒
[if !supportLists]l[endif]TimeUnit.MICROSECONDS;//微妙
[if !supportLists]l[endif]TimeUnit.NANOSECONDS;//納秒
workQueue:一個(gè)阻塞隊(duì)列叭首,用來存儲(chǔ)等待執(zhí)行的任務(wù)习勤,這個(gè)參數(shù)的選擇也很重要,會(huì)對(duì)線程池的運(yùn)行過程產(chǎn)生重大影響焙格,一般來說图毕,這里的阻塞隊(duì)列有以下幾種選擇:
[if !supportLists]l[endif]ArrayBlockingQueue;
[if !supportLists]l[endif]LinkedBlockingQueue;
[if !supportLists]l[endif]SynchronousQueue;
ArrayBlockingQueue使用較少,一般使用LinkedBlockingQueue和Synchronous眷唉。線程池的排隊(duì)策略與BlockingQueue有關(guān)予颤。
threadFactory:線程工廠,主要用來創(chuàng)建線程冬阳;
handler:表示當(dāng)拒絕處理任務(wù)時(shí)的策略蛤虐,有以下四種取值: