Monday, 15 June 2015

java - Control number of parallel Threads -



java - Control number of parallel Threads -

i have many calculations do. run parallel... if there no limit of ram. :/

because of want command number of threads working parallel.

i wrote next code:

list<thread> threadlist = new arraylist<thread>(); threadlist.add(new thread(new runnable() { @override public void run() { .... } })); threadlist.get(threadlist.size()-1).start(); if(threadlist.size() >= 5){ threadlist.remove(0).join(); }

with 1 works fine if there no multithreading. if allow 2 gets crazy , ram getting filled up.

am doing wrong?

please not seek create own thread-list. utilize existing methods , mutual ways doing this.

one proper way using threadpoolexecutor. check that. multithreading vs threadpoolexecutor

you should utilize executor allow limit number of threads used in parallel/max. example:

executorservice executor = executors.newfixedthreadpool(10); executor.submit(yourfirstthread); executor.submit(secondthread); // , on..

java multithreading

No comments:

Post a Comment