This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads Confusion

 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... 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
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic