Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply 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

join() and no reference?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

can you please explain, which thread the join-method is referring to?
I couldn't find any information on join() invoked without a reference...


[ August 15, 2008: Message edited by: Bear Bibeault ]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andreas

The join method is called on instance of a thread object.
For your reference check the API
 
Andreas Auer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abhijeet,

but on which.

I'm wondering cause I was told that the output will 3. So this invocation has to ensure that run-method will run before doSomething adds 1 and prints out i.
What's the order of execution? Or as we're talking about Threads, What could be the order?

Does this mean that in case of two running threads join() waits for the other one to finish, or what?
What if we have multiple threads???

As you can see, I'm a bit confused...

Thanks in advance,
Andreas
[ August 15, 2008: Message edited by: Andreas Auer ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

System.out.printlln(i); in the main method is referring to a non static variable. Compiler error.

Best,
Rudolf
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

this is really tricky question, I try to explain you how it works.
In this line

new MyClass().doSomething();


main thread creates the object of MyClass - at this point the constructor of MyClass is called.
In the constructor value of i is initialized (i=1), then the start() method starts a second thread (start() is implicity called on 'this' object).
Main thread continues to execute, returns from the constructor and then the doSomething() method of MyClass object is called (by main thread).
In doSomething() main thread calls join() method - implicitely on 'this' object. 'this' is the reference to MyClass object (second thread object), so at this point main thread is paused and waits for the second thread to finish.
The second thread calculates the value of 'i' field (i=2) and finishes.
Next, main thread 'wakes up', adds 1 to i (at this point i=2) and prints value of i .... i = 3.

Hope that this is clear enough.
[ August 15, 2008: Message edited by: Ireneusz Kordal ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You also need to remember, that doSomething method will not compile



until You not throw InterruptedException or properly catch inside this method (because it is checked exception).

Bimki
 
Andreas Auer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,


thanks for explanation and hints...

Regards,
Andreas
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic