Java併發 JAVA併發程式設計實戰 讀書筆記14

2021-07-24 06:53:17 字數 3217 閱讀 1789

無論何時,執行緒池需要建立乙個執行緒都要通過乙個執行緒工廠。

public inte***ce threadfactory

預設的執行緒工廠建立乙個新的非守護的執行緒,其中的newthread

會在建立乙個新執行緒時被呼叫。

public class mythreadfactory implements threadfactory

public thread newthread(runnable runnable)

}

上面的例子自定義了乙個執行緒工廠,例項化乙個新的執行緒並傳入池的名稱。

private static volatile boolean debuglifecycle=false;

private static final atomicinteger created=new atomicinteger();

private static final atomicinteger alive=new atomicinteger();

private static final logger log=logger.getanonymouslogger();

this(r,default_name);

} super(runnable,name+」-」+created.incrementandget());

setuncaughtexceptionhandler(

new thread.uncaughtexceptionhandler()});

} public void run()

tryfinally

}} public static int getthreadcreated()

public static int getthreadalive()

public static boolean getdebug()

public static void setdebug(boolean b)

}threadpoolexecutor

的設計是可擴充套件的,他提供了幾個鉤子讓子類覆蓋——

beforeexecute

、afterexecute

和terminated

。執行任務的執行緒會呼叫鉤子函式beforeexecute

和afterexecute

。無論任務時正常從

run中返回還是丟擲乙個異常,

afterexecute

都會被呼叫。如果任務完成後丟擲乙個

error

則afterexecute

不會被呼叫。如果

beforeexecute

丟擲乙個

runtimeexception

,任務將不被執行,

afterexecute

也不會被呼叫。

terminated

。可以用來釋放

executor

在生命週期裡分配到的資源,還可以發出通知、記錄日誌或者完成統計資訊。

public class timingthreadpool extends threadpoolexecutor

protected void afterexecute(runnable r,throwable t)finally

} protected void terminated()finally

}}

把順序遞迴轉換為並行遞迴

publicvoid sequentialrecursive(list> nodes,collectionresults)

}publicvoid parallelrecursive(final executor exec,list> nodes,final collectionresults)

});prarllelrecursive(exec,n.getchildren(),results);

}}publiccollectiongetparallelresults(list> nodes) throws interruptedexception

parallelrecursive

返回的時候,樹上的每個節點都已經被訪問了,遍歷的過程依然是順序的,只是對

compute

的呼叫才是並行執行的。

public inte***ce puzzle

static class node

listasmovelist()

return solution;

}}

順序化解決問題

public class sequentialpuzzlesolver

public listsolve()

private listsearch(nodenode)

for(m move:puzzle.legalmoves(node.pos))}}

return null;

} static class node

}

併發版的謎題解決者

public class concurrentpuzzlesolverfinally

} protected runnable newtask(p p,m m,noden)

class solvertask extends nodeimplements runnable

if(puzzle.isgoal(pos))else}}

}}

使用可攜帶結果的閉鎖

public class valuelatch

public synchronized void setvalue(t newvalue)

} public t getvalue()throws interruptedexception

}}

併發的solver

還不能很好地處理不存在任何方案的情況。

public class puzzlesolverextends concurrentpuzzlesolver

class countingsolvertask extends solvertask

public void run()finally}}

}}

Java併發 ReentrantLock實現分析

reentrantlock是基於aqs實現的可重入獨佔鎖,如果還不了解aqs實現原理的同學可以先去aqs原理分析學習一哈。如果文章中由任何不妥或者謬誤之處,請批評指正。2.公平鎖 3.解鎖過程 reentrantlock核心功能的實現,依賴於繼承aqs類實現的同步器。reentrantlock有三個...

JAVA併發程式設計

通過常量字串 string 來呼叫 wait 或 notify 方法所導致的問題是,jvm 編譯器會在內部自動將內容相同的 string 轉變為相同的物件。這意味著,即便你建立了兩個不同的 mywaitnotify 例項,他們內部的 mymonitorobject 變數也會指向相同的 string ...

Java併發程式設計

執行緒之間通訊 1.加鎖 object.wait 釋放鎖 object.notify 與sychronized 聯合使用,object lock new object sychronized lock sychronized lock 2.改進 無需加鎖並發包下 countdownlatch.awa...