aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads Confusion" Watch "Threads Confusion" New topic
Author

Threads Confusion

Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
Hello All,

I am new to threads and there is a confusion which is confusing me a lot. From K&B book page 691, there is a concept about thread states.
Waiting/Blocked/Sleeping. It says, in these condition the thread is not in the runnable state, but it quotes something like this

"This is the state a thread is in when it's
eligible to run. Okay, so this is really three states combined into one,
but they all have one thing in common: the thread is still alive, but is
currently not eligible to run. In other words, it is not runnable, but it might
return to a runnable state later if a particular event occurs. A thread may be
blocked waiting for a resource (like I/O or an object's lock), in which case the
event that sends it back to runnable is the availability of the resource—for
example, if data comes in through the input stream the thread code is reading
from, or if the object's lock suddenly becomes available. A thread may be
sleeping because the thread's run code tells it to sleep for some period of time,
in which case the event that sends it back to runnable is that it wakes up
because its sleep time has expired. Or the thread may be waiting, because the
thread's run code causes it to wait, in which case the event that sends it back
to runnable is that another thread sends a notification that it may no longer
be necessary for the thread to wait. The important point is that one thread
does not tell another thread to block. Some methods may look like they tell
another thread to block, but they don't. If you have a reference t to another
thread, you can write something like this:
t.sleep(); or t.yield()
But those are actually static methods of the Thread class—they don't affect the
instance t; instead they are defined to always affect the thread that's currently
executing."

In above line it says, one thread may send a notification to the other thread to come in the
runnable state, but then it says, keep in mind no thread can send notification to the other
thread. Lets say, it that resource is no longer needed by the previous thread, then how does
the next thread will know. Will thread scheduler intimate it? Or previous thread will send a
notification and i am not able to understand the concept. Please help me figuring out.


Ben,
--Lost in preparation of SCJP and SCWCD--
"Start writing a new chapter, for if you live by the book you'll never make history." (Ben Sobel)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18853
    
  40

... but then it says, keep in mind no thread can send notification to the other
thread.


Where does it say this???


Lets say, it that resource is no longer needed by the previous thread, then how does
the next thread will know. Will thread scheduler intimate it? Or previous thread will send a
notification and i am not able to understand the concept. Please help me figuring out.


Basically, you have to implement it yourself. If it is important that a resource is tracked, then you need some way to mark that resouce. From the other threads, you have to check that resource before you use it.

The wait and notification mechanism is just a way for threads to wait and send signals -- it can be used for what you want, but doesn't directly do it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
Thanks henry.
I was just confused and misinterpreted as notifications, as it was talking about states. As these all are static methods, so they will apply to the
current execution. I am cleared now. Thanks for the help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Threads Confusion