wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes thread again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "thread again" Watch "thread again" New topic
Author

thread again

VuTuan Tran
Greenhorn

Joined: Oct 21, 2010
Posts: 14
Codes are given from this and I did add some lines of code to know how I understand about the join() method



After running the file, the result is

And then I run the file one more time, the result is total different from the first one

Both of the results are not what I expect and What I am expecting is th2 is still alive


I need some help to make it clear please.Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

VuTuan Tran wrote:
Both of the results are not what I expect and What I am expecting is th2 is still alive


Why? After th2 is started, the main thread is running completely independent from the th2 thread. Why wouldn't th2 finishing before the main thread checks up on it, be an option?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
VuTuan Tran
Greenhorn

Joined: Oct 21, 2010
Posts: 14
Henry Wong wrote:
Why wouldn't th2 finishing before the main thread checks up on it, be an option?
Henry

I think th2 is still alive because I did not invoke t2.join().I am not sure it is right or wrong.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

VuTuan Tran wrote:
I think th2 is still alive because I did not invoke t2.join().I am not sure it is right or wrong.



Well, I'm afraid it's wrong. The join() method is used to cause the current thread to wait for the thread (represented by the thread object) to complete. It has no affect on the thread.

Henry
VuTuan Tran
Greenhorn

Joined: Oct 21, 2010
Posts: 14
Henry Wong wrote:
VuTuan Tran wrote:
I think th2 is still alive because I did not invoke t2.join().I am not sure it is right or wrong.

Well, I'm afraid it's wrong. The join() method is used to cause the current thread to wait for the thread (represented by the thread object) to complete. It has no affect on the thread.
Henry

It is better now. However, there are 2 things I have to get through.Again from my OP, if i name these threads ( just for convenience )

At line 12, join() method is used to cause the current thread to wait for the thread to complete.
So, I know the thread,for sure, is gonna be "fred", but not sure about current thread and complete
Can you tell me which one is the current thread and what do you mean complete here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40


In this case, by "current thread", I mean the thread that called the join() method... which is also the thread that created "fred" and "lucy". By "complete", I mean "finished", "done", etc. -- the run() method has completed, and the threads is no longer considered alive.

Henry
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

The current thread is the thread in which that line of code executes. Which in your uncompilable code snippet is very likely the "main' thread (but could be the EDT if the same code is run via any event listener).


luck, db
There are no new questions, but there may be new answers.
VuTuan Tran
Greenhorn

Joined: Oct 21, 2010
Posts: 14
Henry Wong wrote:
In this case, by "current thread", I mean the thread that called the join() method... which is also the thread that created "fred" and "lucy".

So it is the main thread. isn't it
VuTuan Tran
Greenhorn

Joined: Oct 21, 2010
Posts: 14
Thanks all for your answers. They do help much.
There is one left question about join() is when should we use join() and why should we use it
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
join(), wait() and notify() are used to give order to the threads.

Note: synchronization doesn't give "order".


www.cs-repository.info
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: thread again