user4904366
user4904366

Reputation:

Java wait, notify - wait not working?

I'm learning work with Thread in Java. I am trying to write a program that will have two threads. Each thread will write your name in the cycle. In Main run cycle will generate numbers ( 0 , 9 ). When it generates an odd number, wait the thread 1 ( even - thread 2 ) . If a thread will sleep and wake him up . The problem is that when I call wait , the program freezes . Code:

import java.util.Random;

public class TestClass {


    public static void main(String[] args) throws InterruptedException {

        myThread one = new myThread("Thread one");
        myThread two = new myThread("Thread two");

        one.start();
        two.start();

        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            int rnd = random.nextInt(10);
            if (rnd % 2 == 0) {
                synchronized (two) {
                    two.wait(); // first loop - freez
                }
            } else {
                synchronized (one) {
                    one.wait();
                }
            }
        }

    }

}

class myThread extends Thread {

    private boolean canIRun = true;
    private final String name;

    myThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (canIRun) {
            System.out.println("My name is: " + name);
        }
    }

}

Upvotes: 0

Views: 574

Answers (2)

codeDr
codeDr

Reputation: 1684

I wanted to create an answer that re-broadcasts @jonskeet's comment above:

Do not call wait() or notify() on Thread objects - it's unfortunate, but Thread uses its own monitor for other things.

I have found this out several times the hard way in classes that extend Thread or implement Runnable that were hung because of synchronized(this). Since finding this question, I have searched many times and in nowhere have I found any warning to not do this. And if you have a fast, idle computer, you will probably never find this in testing.

Perhaps someone with better google-foo can find the documentation. If so, please comment or amend this answer.

wait notify threads synchronized this hung thread

Upvotes: 0

Thilak
Thilak

Reputation: 726

You have three threads in your program, Main thread is where the program starts executing by entering the main() method. Main thread creates two threads namely Thread One and Thread Two. Thread one and two runs their run() method. Main thread is running main() method. When you call one.wait() in main() method, it pauses the current thread, which is Main thread. It wont pause Thread one. the same is true for two.wait(), it pauses the Main thread too. Now the Main thread can only resume, if any other thread calls notify() or notifyAll() on the same object either one or two, whichever object made it to suspend.

In your program, Main thread freezes, but the other two thread executes continuously until the program is terminated.

Upvotes: 1

Related Questions