File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes join doubts in Thread 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 doubts in Thread" Watch "join doubts in Thread" New topic
Author

join doubts in Thread

Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344
This is a general pgm for play?( ) with threads written by me and not taken from any other source..




How join is working in the above coding? I am having three threads t,t1 and t2. Suppose I want thread t should complete then only thread t2 should start means, how can i use join.. ? Whether join should be used after the thread has been started like t.start()?

And also, In yield() it will send back the currently running thread to the runnable pool and select any one of the threads from the pool of the same priority of previously running thread...correct. What it will happen if yield is used but there is no thread of same priority in the pool.(assuming in the runnable pool all the thread are having priority of 5 and currently running thread is having priority of 6...)
[ December 09, 2006: Message edited by: Micheal John ]

Micheal John
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

Hi Michael John,

Join simply says: Block this thread until the thread I called join on has stopped his run method.
If you want t2 to start only after t has done his thing then you must write:
t.join //block until t is done
t2.start();//now start t2

You should use your join after the thread has started otherwise it will do nothing (won't block either, just nothing.).

Yield will do whatever it feels like. You can not say with certainty what it will do. It MIGHT send back the current thread to runnable state, but it also might not and leave the current one running. You are just requesting the VM to have another look at your threads if it might not be interesting to let another one run but you can't force anything. (a bit like with System.gc(), you can't force the garbage collector either.) But if I were you I wouldn't worry so much about the yield , knowing that you can't force anything is the most important I think.

regards,
Mark


I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

Micheal John posted December 09, 2006 01:50 AM
I am having three threads t,t1 and t2.

No, you have four. The three you mentioned and the main thread.

yx.join();
will always make the current thread wait for the thread calling join (xy here) to complete and die.

The current thread in your example is "main" in both calls. And really, the output from the main thread only occurs after t (Thread-0) and t2 (Thread-2) are dead. Output was:
I am ::Thread-0
I am ::Thread-0
I am ::Thread-0
I am ::Thread-0
I am ::Thread-0
I am ::Thread-0
I am ::Thread-0
I am ::Thread-0
I am ::Thread-0
I am ::Thread-0
I am ::Thread-2
I am ::Thread-2
I am ::Thread-2
I am ::Thread-2
I am ::Thread-2
I am ::Thread-2
I am ::Thread-2
I am ::Thread-2
I am ::Thread-2
I am ::Thread-2
I'm in Main Stack5
I am ::Thread-1
I am ::Thread-1
I am ::Thread-1
I am ::Thread-1
I am ::Thread-1
I am ::Thread-1
I am ::Thread-1
I am ::Thread-1
I am ::Thread-1
I am ::Thread-1

There is no Thread-0 or Thread-2 after the Main Stack message.
Without the joins, the message from the main thread would be much earlier.

Michael also wrote:
Suppose I want thread t should complete then only thread t2 should start means, how can i use join.. ?

as Mark wrote:
t.join //block until t is done
t2.start();//now start t2

if this lines are in the main method, main is the current thread.
main blocks until t is dead (poor t) and then proceeds to the second line and starts t2.



For yield, as Mark said you cannot be very certain. But in most cases, when there is no thread with the same or higher priority, yield basically does nothing.

Here's an example with 24 low and one high priority threads,
the yield is called on the high priority thread and on one of the low priority threads.
At least on my system, only the the yield call on one of the low priority threads has an effect, as you can see by comparing the outputs when commenting out the yield (and the if clause, arrow):


output:
!!!.
AAAAAAAAAA.
BBBBBBBBBB.
CCCCCCCCCC.
EEEEEEEEEE.
FFFFFFFFFF.
GGGGGGGGGG.
HHHHHHHHHH.
IIIIIIIIII.
JJJJJJJJJJ.
KKKKKKKKKK.
LLLLLLLLLL.
MMMMMMMMMM.
NNNNNNNNNN.
OOOOOOOOOO.
PPPPPPPPPP.
QQQQQQQQQQ.
RRRRRRRRRR.
SSSSSSSSSS.
TTTTTTTTTT.
UUUUUUUUUU.
VVVVVVVVVV.
WWWWWWWWWW.
XXXXXXXXXX.
DDDDDDDDDD.


The DDD thread yields, but the !!! doesn't.

When commenting out the line with the yield, the !!! stay where they are, but the DDDDD will be before the EEEE.


Yours,
Bu.


all events occur in real time
Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344
Thanks Mark Uppeteer and Burkhard Hassel for your replies
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: join doubts in Thread