Hi. I am working on a multi threaded program (game renderer). What I want to do is to loop through my threads and get a boolean value that tells me if the thread is done rendering or not. If all the threads is done, I want the code to go on, otherwise wait. I thought I had this locked down, but it does not work. The threads work, I have checked this with sysout and such. The issue is my method of looping through them I suspect.
//this is in the draw loop of the main drawer:
while(stillDrawing){
//My idea is to set the state of the while loop to "continue", or "done", and change this
// this if it turns out if one or more of the threads is not done.
stillDrawing = false;
//looping through my list of threads
for (DrawerThread thread : threads) {
//setting the while loop to True if the thread is not waiting, iow. is running
if(!thread.isWaiting())
{
stillDrawing = true;
}
}
//simply wait a little if we find that not all threads is done
if(stillDrawing){
try {
this.wait(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//Sysout the threads, and their states. The states are not changed until the a render method is called.
for (DrawerThread thread : threads) {
System.out.println("Thread "+thread.getId()+" is waiting: "+thread.isWaiting()+" in frame loop nr: "+loop);
}
Am I doing the basic logics wrong, if so, how to make this work? My rendering logics is dependent on these threads being done before the code continues. Here is a little of the console when it renders:
"
Thread 0 is waiting: false in frame loop nr: 87
Thread 1 is waiting: false in frame loop nr: 87
Thread 2 is waiting: true in frame loop nr: 87
Thread 3 is waiting: false in frame loop nr: 87
Thread 0 is waiting: false in frame loop nr: 88
Thread 1 is waiting: false in frame loop nr: 88
Thread 2 is waiting: false in frame loop nr: 88
Thread 3 is waiting: true in frame loop nr: 88
Thread 0 is waiting: false in frame loop nr: 89
Thread 1 is waiting: false in frame loop nr: 89
Thread 2 is waiting: false in frame loop nr: 89
Thread 3 is waiting: false in frame loop nr: 89
"
As we can see. Most of the threads is not actually done. I have double checked that I do not start the rendering again before this step is done (also, it is synchronized). It is also only the start rendering sequence that will set the waiting status to false, so the value is not ever changed to anything else than true when this check is running.
Thanks for the help:)