-
線程池:線程的創(chuàng)建和銷毀時(shí)間大于執(zhí)行時(shí)間
http://blog.csdn.net/hsuxu/article/details/8985931
executor框架:http://www.cnblogs.com/MOBIN/p/5436482.html
將提交任務(wù)的線程和執(zhí)行任務(wù)的線程解耦,采用生產(chǎn)者消費(fèi)者模式猫态。
并用Runnable來表示任務(wù)。
executors:提供一系列靜態(tài)工廠方法來創(chuàng)建線程池。
newFixedThreadPool:創(chuàng)建固定數(shù)量的線程池
newScheduledThreadPool:創(chuàng)建可延時(shí)執(zhí)行或定時(shí)執(zhí)行的線程池
newCachedThreadPool:再一定時(shí)間內(nèi)未使用的線程會(huì)被移除暂吉,有之前創(chuàng)建的可用線程就重用孽尽,否則新建珠增。
線程池使用舉例:
public class ThreadPoolDemo {
static class Task implements Runnable{
private String id;
Task(String id){
this.id = id;
}
@Override
public void run() {
System.out.println("Thread "+id+" is working");
try {
//每個(gè)任務(wù)隨機(jī)延時(shí)1s以內(nèi)的時(shí)間以模擬線程的運(yùn)行
Thread.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread "+id+" over");
}
}
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(3);//線程池中,3工作線程
threadPool.execute(new Task("a"));
threadPool.execute(new Task("b"));
threadPool.execute(new Task("c"));
threadPool.execute(new Task("d"));
threadPool.execute(new Task("e"));
threadPool.shutdown();
while(!threadPool.isTerminated()){
}
System.out.println("Thread Pool is over");
}
}
-
concurrenthashmap
http://blog.csdn.net/yansong_8686/article/details/50664351
-
volidate關(guān)鍵字
線程棧(線程的工作內(nèi)存)保存了線程運(yùn)行時(shí)候變量值信息专普。當(dāng)線程訪問某一個(gè)對(duì)象時(shí)候值的時(shí)候,首先通過對(duì)象的引用找到對(duì)應(yīng)在堆內(nèi)存的變量的值弹沽,然后把堆內(nèi)存變量的具體值load到線程本地內(nèi)存中檀夹,建立一個(gè)變量副本,之后線程就不再和對(duì)象在堆內(nèi)存變量值有任何關(guān)系策橘,而是直接修改副本變量的值炸渡,在修改完之后的某一個(gè)時(shí)刻(線程退出之前),自動(dòng)把線程變量本的值回寫到對(duì)象在堆中變量丽已。這樣在堆中的對(duì)象的值就產(chǎn)生變化了蚌堵。
-
關(guān)于REENTRANTLOCK和SYNCHRONIZED