-2

How should the code below perform if executed randomly at different time?

public class Unstoppable extends Thread {
    private int counter;

    @Override
    public void run() {
        synchronized(this) {
            for(int i = 0; i < 10; i++)
                counter++;

            this.notifyAll();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Unstoppable unStoppable = new Unstoppable();

        System.out.println("I am about to start");
        unStoppable.start();
        synchronized(unStoppable) {
            System.out.println("I was just told to wait");
            unStoppable.wait();
        }

        System.out.println(unStoppable.counter);
    }
}

At the first look at this it seemed like it would hang infinitely but quiety surprisingly every time I execute this...It completes execution.

10
  • you wait on a condition in loop. also unstoppable unstoppable = new unstoppable(); does not follow proper naming convention for a class.
    – Nishant
    Commented Nov 18, 2013 at 17:10
  • Meanwhile in a parallel universe...
    – Adam Arold
    Commented Nov 18, 2013 at 17:11
  • 2
    Calling the class 'unstoppable` with all-lower-case is just wrong. Calling the instance the exact same name and case as the class is double-wrong.
    – rolfl
    Commented Nov 18, 2013 at 17:12
  • Waits forever for me. What a waste of time! Commented Nov 18, 2013 at 17:17
  • 1
    there is just one thread....increase number of threads and it will wait.
    – Nishant
    Commented Nov 18, 2013 at 17:28

3 Answers 3

2

Synchronization is meaningful if you have two or more threads. In your case there is just one thread.

 public static void main(String[] args) throws InterruptedException {
    Unstoppable unStoppable = new Unstoppable();
    Unstoppable unStoppable2 = new Unstoppable();
    Unstoppable unStoppable3 = new Unstoppable();
    System.out.println("I am bout to start");
    unStoppable.start();
    unStoppable2.start();
    unStoppable3.start();
    synchronized (unStoppable) {
        System.out.println("I was just told to wait");
        unStoppable.wait();
    }

    System.out.println(unStoppable.counter);
}

}

execute this at random times it might wait or try to increase the number of threads. By the way wait should be implemented like this.

 synchronized (obj) {
     while (<condition does not hold>)
         obj.wait();
     ... // Perform action appropriate to condition
  }
0

Are you ignoring exceptions somehow.... the code:

// synchronized(unstoppable) {
    System.out.println("I was just told to wait forever");
    unstoppable.wait();
// }

Should throw an IllegalMonitorStateException and immediately exit.


EDIT

So you changed your code, and now it works the way you expect (it waits forever), so what's the problem? (I just tested it).

5
0

At the first look at this it seemed like it would hang infinitely but quiety surprisingly every time I execute this...It completes execution.

It completes because when a Thread finishes, there is a behind-the-scenes call to thread.notify(). This is how thread.join() is implemented.

That said, it is not a good idea to depend on the code working in this manner -- or at least is should be labeled as a hack. You should use thread.join() instead:

unStoppable.join();

Lastly, you should consider making your Unstoppable class implement Runnable instead of extending thread. That's a better pattern:

public class Unstoppable implements Runnable {
...

Thread unStoppableThread = new Thread(new Unstoppable());
...
unStoppableThread.join();
4
  • 1
    his question has changed so many times that your answer, although it is correct for this version of the code, is not correct for the first number of versions. Specifically, in all but the last version of his question he had a Thread.sleep(10000) before the wait() which would essentially guarantee a 'forever' wait.
    – rolfl
    Commented Nov 18, 2013 at 18:38
  • Yeah I saw that @rolfl. I certainly didn't down vote your answer if that was the implied question. :-)
    – Gray
    Commented Nov 18, 2013 at 18:40
  • 1
    Nah, downvoting does not phase me. I am really just annoyed that the OP has wasted lots of people's time by messing around with the question.... feels like he doesn't deserve it....
    – rolfl
    Commented Nov 18, 2013 at 18:42
  • Agreed @rolfl. I typically assume they don't know what they are doing wrong. 95% on SO don't realize that they should be asking/answering for posterity and not for their little pet issue.
    – Gray
    Commented Nov 18, 2013 at 18:46

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.