0

I am new to JAVA and while studying JAVA complete reference I came across this code

class DemoThread implements Runnable {
  String name; // name of thread
  Thread t;

  DemoThread(String threadname) {
    name = threadname;
    t = new Thread(this, name);
    System.out.println("New thread: " + t);
    t.start(); // Start the thread
  }

  // This is the entry point for thread.
  public void run() {
    try {
      for(int i = 5; i > 0; i--) {
        System.out.println(name + ": " + i);
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      System.out.println(name + " interrupted.");
    }
    System.out.println(name + " exiting.");
  }
}

class DemoJoin {
  public static void main(String args[]) {
    DemoThread ob1 = new DemoThread("One");
    DemoThread ob2 = new DemoThread("Two");
    DemoThread ob3 = new DemoThread("Three");

    System.out.println("Thread One is alive: "
                        + ob1.t.isAlive());
    System.out.println("Thread Two is alive: "
                        + ob2.t.isAlive());
    System.out.println("Thread Three is alive: "
                        + ob3.t.isAlive());
    // wait for threads to finish
    try {
      System.out.println("Waiting for threads to finish.");
      ob1.t.join();
      ob2.t.join();
      ob3.t.join();
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }

    System.out.println("Thread One is alive: "
                        + ob1.t.isAlive());
    System.out.println("Thread Two is alive: "
                        + ob2.t.isAlive());
    System.out.println("Thread Three is alive: "
                        + ob3.t.isAlive());

    System.out.println("Main thread exiting.");
  }
}

The supposed output given in book is

New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Two: 3
Three: 3
One: 2
Two: 2
Three: 2
One: 1
Two: 1
Three: 1
Two exiting.
Three exiting.
One exiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false
Main thread exiting.

But when I ran this code in Netbeans I got

run:
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
One: 5
Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Three: 3
Two: 3
One: 2
Three: 2
Two: 2
One: 1
Three: 1
Two: 1
One exiting.
Three exiting.
Two exiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false
Main thread exiting.
BUILD SUCCESSFUL (total time: 5 seconds)

How come One: 5 came in between New thread: Thread[Three,5,main] and Thread One is alive: true

5
  • 1
    What is the context of this example? Unless I'm missing something that code shouldn't guarantee the order of thread execution (which means multiple runs can result in different outputs). Commented Dec 20, 2018 at 1:14
  • This is an example for using isAlive( ) and join( ). But my query is why the sequence was not followed and first thread started running after instantiation. Commented Dec 20, 2018 at 1:18
  • The example is invalid. There is nothing in this code that guarantees that output. Commented Dec 20, 2018 at 1:24
  • The constructor of DemoThread starts its member Thread. The "is alive" statements happen after the constructor calls. Which print statements are invoked first is dependent on thread scheduling as the given code does not guarantee order. Commented Dec 20, 2018 at 1:26
  • You mention the example is supposed to show isAlive and join use; it does that. But does the example actually guarantee the same output every time? Because if it does then user207421 is right and the example is invalid (or at least partially wrong). Commented Dec 20, 2018 at 1:48

1 Answer 1

1

There is only one thing which can be said about this example is - the Main thread will exit only after completion of thread one, two and three. You can not guarantee the order of execution of Thread One, Thread Two, and Thread Three. In fact, you run this code multiple times you may see different order in different execution.

New thread: Thread[Three,5,main] -> Means main thread has spawned the third thread. By this time all the three threads have been spawned and they may be running already.

One: 5 -> Thread One is executing

Thread One is alive: true -> This is something which is executed in the Main thread. There are chances that you may have seen this after seeing the following lines:

Two: 5

Three: 5

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

1 Comment

Yes it gives different sets of results everytime. But when I am studying the book its not clearly mentioned that only that sequence will follow. I thought when we call DemoThread ob1 = new DemoThread("One"); the object is created but the run method doesnt start. I thought ob1.t.join(); starts running the thread. So there was my confusion.

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.