*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in Threading - 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in Threading - 2" Watch "Problem in Threading - 2" New topic
Author

Problem in Threading - 2

Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
Another one from Niko's blog (Q 66)

The output of above code is 99999.
Shouldn't the main thread wait forever as no other thread is calling notify() or notifyAll() on the thread object created in main method ?
Or is it that notify() is automatically called when the run method finishes?


Cheers,
Divyesh.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

wait() means wait(0). So, check this API.

There, they've said,

The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.

The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked.


So, I think, after the timeout, the thread will be in the thread pool to acquire the lock of that object in normal way. And it acquires it!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
I'm sorry Abimaram, but your answer is misleading. Divyeshh is correct that
the wai() will last until the object is notified; for ever if necessary. The problem
here is that when wait() is called on an object of type Thread, special rules apply.

Try the code below. You will see that the wait() does last for ever when a simple
lock object is used. I will leave it to someone more expert in the behavior of type
Thread to explain why this type does not wait. Jim ... ...


BEE MBA PMP SCJP-6
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
This complicated topic used to be on the SCJP exam, but it isn't any more... hooray!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Jim Hoglund wrote:I'm sorry Abimaram, but your answer is misleading.


May be, I'm also searching answer for this question from my SCJP preparations. So, let's see, what others say! May be Henry Wong will give the suitable answer!

Thanks!
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Bert : Has threading been removed from the exam? It must have
happened just now since I sat for SCJP-6 in mid July and it was
definitely there. It was my best topic (100%).

Jim ... ...
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
Bert Bates wrote:This complicated topic used to be on the SCJP exam, but it isn't any more... hooray!

Thanks for the information Bert.
But my doubt still persists .
Need to know what is special about threads that the main thread doesn't need a call to notify() or notifyAll() to come out of wait().
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

really a challenging question..


SCJP6.0,My blog Ranchers from Delhi
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
Looks like it has something to do with start() method.
The wait() on thread ends automatically only when start()[ultimately run()] finishes execution.
If there is no call to start() method on the thread, the main thread waits forever...
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

i Donot think so that it should have any relation with the start.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Divyeshh : In the first post the wait() was on an object that extends Thread,
not the "main" thread. So I think the mystery notify() is a behavior of type
Thread. We should study Thread to find the answer.

Jim ... ...
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
Jim Hoglund wrote:Divyeshh : In the first post the wait() was on an object that extends Thread,
not the "main" thread. So I think the mystery notify() is a behavior of type
Thread. We should study Thread to find the answer.

Jim ... ...

Yes Jim, I meant Thread only when I wrote that it has something to do with start() method (yes, i can know you can define a method with any name, within java's rules of course, but I was thinking of the already existing methods )
The main thread ends only if you have called the start() method of the Thread on which the main thread is waiting, otherwise it does not end. So probably as I had mentioned in my first post, something is happening when the run() method finishes. [Remember, calling the run() method directly also does not end the main thread, run() has to be called via start() only]
Or is it that notify() is automatically called when the run method finishes?

Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Divyeshh Patel wrote:
... The main thread ends only if you have called the start() method of the Thread on which the main thread is waiting,
otherwise it does not end. So probably as I had mentioned in my first post, something is happening when the run()
method finishes. [Remember, calling the run() method directly also does not end the main thread, run() has
to be called via start() only]

I'm still confused about the meaning of "main thread". Can you explain further?

Jim ... ...
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

I believe it is because the run() method of a Thread object calls notifyAll() before it returns. So the mistake was to synchronize on a Thread object.

What happens:
-The main thread starts waiting for the Thread-0 to return
-Thread-0 calls notifyAll() and returns
-The main thread wakes up and finishes.

Essentially, the OP did what happens when you call t.join()



http://www.coderanch.com/t/233226/threads/java/we-thread-object-as-lock


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
Jim Hoglund wrote:
I'm still confused about the meaning of "main thread". Can you explain further?

Jim ... ...

By main thread I mean the thread that starts when you execute the program(and which executes the main() method)
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
Thanks for the pointer Dieter.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Thanks to all. Looks like another reason to implement
Runnable, rather that extend Thread. Ciao...

Jim ... ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in Threading - 2