I have a many calculations to do. All could run parallel... if there where no limit of RAM. :/
Because of that i want to control the number of threads working parallel.
I wrote the following 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 this works fine as if there where no multithreading. But if i allow 2 it gets really crazy and the RAM is getting filled up.
Am i doing something wrong?