Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to introduce the code related to Java multithreaded loop

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to introduce the code related to Java multithreaded loop, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The Java multithreaded loop requires us to keep learning, and there are many issues that we care about each other all the time. Let's take a look at how to better use this programming language. When each iteration is independent of each other and completing the work of each iteration in the loop body is significant enough to cover the overhead of managing a new task, this sequential loop is suitable for parallelization.

Public voidParallelRecursive (final Executorexec

Listnodes,Collection results) {

For (Node n:nodes) {

Exec.execute (new Runnable () {

Public void run () {

Results.add (n.compute ())

}

});

ParallelRecursive (exec,n.getChildren (), results)

}

}

PublicCollectiongetParallelResults (Listnodes)

Throws InterruptedException {

ExecutorService exec=Executors.newCachedThreadPool ()

Queue resultQueue=newConcurrentLinkedQueue ()

ParallelRecursive (exec,nodes,resultQueue)

Exec.shutdown ()

Exec.awaitTermination (Long.MAX_VALUE,TimeUnit.SECONDS)

Return reslutQueue

}

However, the above procedures cannot deal with situations where there is no solution, and the following procedures can solve this problem.

Public class PuzzleSolverextendsConcurrent

PuzzleSolver {

...

Privatefinal AtomicInteger taskCount=new AtomicInteger (0)

ProtectedRunnable newTask (P pmae M m M J J Noden) {

Return new CountingSolverTask (pdym _ m _ n)

}

ClassCountingSolverTask extends SolverTask {

CountingSolverTask (P pos,Mmove,Node prev) {

Super (pos,move,prev)

TaskCount.incrementAndGet ()

}

Publicvoid run () {

Try {

Super.run ()

}

Finally {

If (taskCount.decrementAndGet () = = 0)

Solution.setValue (null)

}

}

}

}

On how to carry out Java multithreaded loop related code introduction is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report