0

I have a Java program which creates two threads each one executing the same code (the same run()).

My first Thread1 executes wait() on some monitor and is suspended until the second thread Thread2 calls notify on the same monitor.

My main looks like:

{
            // Create threads   
            GameOfLifeThread[][] threads = new GameOfLifeThread[vSplit][hSplit];        
            for(int i=0; i<vSplit; i++){
                for(int j=0; j<hSplit; j++){
                    threads[i][j] = new GameOfLifeThread(initalField, ...);
                }
            }       
            // Run threads      
            for(int i=0; i<vSplit; i++){
                for(int j=0; j<hSplit; j++){

                    threads[i][j].run();
                }
            }   

            return ...;
}

The run() function looks like:

{
    ...
    synchronized (bordersReadyForRead) { 
                    ...                     
                    bordersReadyForRead.wait();
}
            ...                                 
}

The main thread continues to execute the run() of the first thread created and goes waiting. By some unknown reason the second thread doesn't starts at all!

What can be the reason for this problem?

Thank you in advance.

1 Answer 1

3

Start your threads with "start", not "run".

What's happening is "run" just calls your run method, so it goes to the block and waits for the other thread. If you use "start", then a new thread is launched, and your program (may) work as expected.

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer was very helpful. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.