• 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

Problem in Threading - 2

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ... ...
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This complicated topic used to be on the SCJP exam, but it isn't any more... hooray!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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().
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
really a challenging question..
 
Divyeshh Patel
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i Donot think so that it should have any relation with the start.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ... ...
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()



https://coderanch.com/t/233226/threads/java/we-thread-object-as-lock
 
Divyeshh Patel
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the pointer Dieter.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all. Looks like another reason to implement
Runnable, rather that extend Thread. Ciao...

Jim ... ...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic