Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread again

 
VuTuan Tran
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
VuTuan Tran
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
VuTuan Tran
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
join(), wait() and notify() are used to give order to the threads.

Note: synchronization doesn't give "order".

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic