wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes join Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "join" Watch "join" New topic
Author

join

Dimple Kaushik
Ranch Hand

Joined: Nov 19, 2003
Posts: 49
public class Joining {
static Thread createThread(final int i, final Thread t1) {
Thread t2 = new Thread() {
public void run() {
System.out.println(i+1);
try {
t1.join();
} catch (InterruptedException e) {
}
System.out.println(i+2);
}
};
System.out.println(i+3);
t2.start();
System.out.println(i+4);
return t2;
}
public static void main(String[] args) {
createThread(10, createThread(20, Thread.currentThread()));
}
}
Can anybody explain me how this executes.
I thought 23 is printed first and 14 last but actually 12 is the last one to print. How?
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Hi, it's a rather interesting question with an unguaranteed output.

About the only outcome to be guaranteed is that the output will start with 23. Every execution. The flow of control is as follows:
createThread() is called from main(), its second argument is evaluated and resolved first resulting in another call to createThread(). Thread.currentThread() returns a reference to main() thread. Then, an object of an anonimous class of Thread type is created in createThread() method. Here's the guaranteed part:

i is equal to 20, so 23 will be printed to the console. After this line, everything else is open game and up for grabbs. The scheduler may choose to immediately run t2 thread, which will result in 21 being printed. A call to t1.join() to me is senseless since t1 is really main() and cannot be completed before the outer createThread() executes. Or, the scheduler may decide to procede with the first createThread() and print out 24 after 23. At this point, t2 is waiting patiently in a runnable pool for its turn to run. When it will happen to run, is anybody's guess.
OK, back to the inner createThread(). It returns a Thread object as the second argument to the outer createThread() call. Now, a new object of an anonimous class gets created as the outer createThread() call is made. 13 will be printed out for sure, but we don't know anything about the preceding sequence. t2 thread object will be put to the runnable state (maybe, the previous thread object will have completed by then, maybe not). This new t2 thread object may be selected to run immediately or afterwards depending on the scheduler. 14 gets printed out at some point. 11 and 12 are also part of the output.
This is about as close to the guarantee as you can get in this example:
  • 23 will be printed first
  • the program will complete normally
  • 11, 12, 13, 14, 21, 22, 23, 24 will always be part of the output

  • No other statement about the order of flow control can be made in my view. Don't forget that a thread can be withheld from the running state to runnable just because the scheduler feels like it. I'd be glad to discuss different opinions on that.
    Marlene Miller
    Ranch Hand

    Joined: Mar 05, 2003
    Posts: 1391
    Vad, you were faster than me. I hope you don't mind if I still post my explanation.
    I am going simplify your example just a little bit.

    The static method createThread is invoked. An anonymous subclass of Thread is instantiated. 10+3 == 13 is printed. The new thread is started.
    Now there are two threads: (1) the main thread which is executing the static method and (2) the new thread which will execute run(). Either thread could execute. Either the main thread will print 10+4 == 14 or the new thread will execute 10+1 == 11.
    Then the new thread will wait for the main thread to return from the static method and return from the main method and terminate. Then the new thread will print 10+2 == 12.
    So one possible output is 13 14 11 12. Another possible output is 13 11 14 12.
    Vad Fogel
    Ranch Hand

    Joined: Aug 25, 2003
    Posts: 504
    Originally posted by Marlene Miller:
    Then the new thread will wait for the main thread to return from the static method and return from the main method and terminate. Then the new thread will print 10+2 == 12.

    Marlene, I just don't see how you can make the main() thread run to its completion while its arguments are still evaluated?
    To me, t1.join() cannot make main() complete since createThread() must return an object to main() and therefore terminate before main() is done.
    Dimple Kaushik
    Ranch Hand

    Joined: Nov 19, 2003
    Posts: 49
    Thanks for your explanations.
    Originally posted by Vad Fogel:

    This is about as close to the guarantee as you can get in this example:
  • 23 will be printed first
  • the program will complete normally
  • 11, 12, 13, 14, 21, 22, 23, 24 will always be part of the output

  • No other statement about the order of flow control can be made in my view. Don't forget that a thread can be withheld from the running state to runnable just because the scheduler feels like it. I'd be glad to discuss different opinions on that.

    Even I thought the same but when i tried running the code couple of times, everytime my output started with 23 and ended with 12 which means to me as that it is very guaranteed.But how??
    Originally posted by Marlene Miller:
    I am going simplify your example just a little bit.


    This is clear to me as there is only one call to createThread.
    Vad Fogel
    Ranch Hand

    Joined: Aug 25, 2003
    Posts: 504
    What may seem to be guaranteed on one OS, will gladly behave differently on the other OS. For the exam, you want to get only trustful and guaranteed statements on the Thread and GC behavior patterns. You will be tested on whether you fully understand this concept. As I mentioned before, there's nothing in this example to me that can suggest any overall guaranteed control flow other than 23 being printed first.
    Dimple Kaushik
    Ranch Hand

    Joined: Nov 19, 2003
    Posts: 49
    Thanks Vad
    Marlene Miller
    Ranch Hand

    Joined: Mar 05, 2003
    Posts: 1391
    Time and sequential order are vertical. Concurrency is horizontal more or less.

    [ November 26, 2003: Message edited by: Marlene Miller ]
    Marlene Miller
    Ranch Hand

    Joined: Mar 05, 2003
    Posts: 1391
    I am trying to find a way to express sequential order (horizontal) and concurrency (vertical).

    [ November 26, 2003: Message edited by: Marlene Miller ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: join
     
    Similar Threads
    Joining Thread, Explanation required
    Joining threads
    threads
    A hard question
    output of this program from certification book