Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about the join() in thread

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Given from scjp


There are some questions which i made up myself to test how i understand about the thread issue and I really need some experts to confirm about it.

Question 1 :At line 3, how many threads we have now ?
my answer is

Question 2 : At line 4, after t1.start(), which thread will be execute first.
My answer is
Question 3 :what t1.join() does ?
my answer is
What I am thinking is t.join() joins the current thread to the end of t so that when t finishes the current thread can run again. However, which one is the current thread now. That is why I am stuck
Please help me. I appreciate
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

VuTuan Tran wrote: .... Question 2 : At line 4, after t1.start(), which thread will be execute first ....


The main thread will execute first, and if you ask which thread will complete execution first, then its t1 since t1.join causes main to wait for t1 finish execution..

VuTuan Tran wrote: .... Question 3 :what t1.join() does ? ....


t1.join() interrupts main() from being runnable and main waits for t1 to complete its execution ..
 
VuTuan Tran
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Nipun Arora wrote:

VuTuan Tran wrote: .... Question 2 : At line 4, after t1.start(), which thread will execute first ....


The main thread will execute first, and if you ask which thread will complete execution first, then its t1 since t1.join causes main to wait for t1 finish execution..

VuTuan Tran wrote: .... Question 3 :what t1.join() does ? ....


t1.join() interrupts main() from being runnable and main waits for t1 to complete its execution ..


I doubt main thread will execute first. There is another issue I should make it clear is

Given the preceding code from OP, what our current thread is
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

VuTuan Tran wrote:Question 1 :At line 3, how many threads we have now ?

Have a play with the visualvm tool that comes with the JDK. It gives a nice display of what threads are running and what state they are in.

VuTuan Tran wrote:I am not sure about the t thread because in the book, they indicate that after instantiating the thread,
we just have a thread object but not a true thread.

'they' are correct

VuTuan Tran wrote:which one is the current thread

The current thread is the thread that is running that piece of code.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The code does not compile, join() may throw InterruptedException which is not caught.

VuTuan Tran wrote:
Question 3 :what t1.join() does ?


t1.join() waits for t1 to complete.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Henry Wong wrote:Please QuoteYourSources

Agree. I am closing this thread. Please PM me with the source, then I shall reopen this thread.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic