Java程序多線程遞歸彌補(bǔ)管理漏洞

字號(hào):


    Java多線程遞歸在我們使用的時(shí)候需要我們不斷的進(jìn)行學(xué)習(xí),其實(shí)每個(gè)語(yǔ)言都可以在源代碼中找到問題的解決方案。當(dāng)每個(gè)迭代彼此獨(dú)立,并且完成Java多線程遞歸中每個(gè)迭代的工作,意義都足夠重大,足以彌補(bǔ)管理一個(gè)新任務(wù)的開銷時(shí),這個(gè)順序循環(huán)是適合并行化的。
    1.public voidParallelRecursive(final Executorexec,
    List
    2.for(Node n:nodes){
    3.exec.execute(new Runnable(){
    4.public void run(){
    5.results.add(n.compute());
    6.}
    7.});
    8.parallelRecursive(exec,n.getChildren(),results);
    9.}
    10.}
    11.publicCollectiongetParallelResults(List
    12.throws InterruptedException{
    13.ExecutorService exec=Executors.newCachedThreadPool();
    14.Queue resultQueue=newConcurrentLinkedQueue();
    15.parallelRecursive(exec,nodes,resultQueue);
    16.exec.shutdown();
    17.exec.awaitTermination(Long.MAX_VALUE,TimeUnit.SECONDS);
    18.return reslutQueue;
    19.}
    但是Java多線程遞歸程序不能處理不存在任何方案的情況,而下列程序可以解決這個(gè)問題。