Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Summary of wait() notify() join() and synchronized

 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone summarize the relationships among the Object
methods wait() and notify(), the Thread method join() and key
word synchronized. (Am I missing any?) Thanks.
Jim ...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

This is a pretty big topic; it has spawned many books. If I were you, I'd set aside an afternoon, and read though this tutorial at Sun's site, which is not only quite good, it's also quite modern -- it teaches you what you need to know in the most up-to-date fashion possible.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest : You bet I know that it's a huge topic. And
I've spent many afternoons (and nights) studying,
using and abusing these tools. I can write my own
summary, but a more expert view would sure be
helpful. Seriously, 100 words or less, please. I will
be studying the concurrency tutorial also.
Jim ...
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it would be better for you to post your specific confusions about the topic after you've read the tutorials rather than asking a volunteer to try to write a chapter to a book that is not likely to be better than the tutorials. Don't you agree?
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I will do my own 100 word summary for your
comment. I'm not particularly confused but trying to
consolidate my high-level understanding of the Java
OO approach. Thanks for responding and stay tuned.
Jim...
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Hoglund wrote:Okay, I will do my own 100 word summary for your
comment.


Cool. I for one will look forward to this.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay Pete here it is - about 200 words, however.
Jim ... ...

Threads & Data Consistency

An executing thread is time sliced by the JVM, from start() until the end of its
run() method. To run a thread after another thread completes, just join() the
other thread. Call yield() to pause a thread. This relinquishes some of its time
slices for use elsewhere. For consistency of shared data, a thread can check
the data, sleep() and then check again, until the data is ready. Except join(),
these Thread methods operate on Thread.currentThread().

To coordinate thread activities, or wait for consistent shared data, call wait() on
a shared object. Then, when an active thread calls notify() on the same object,
you can proceed, or check the shared data and then proceed. This works like
the check and sleep() loop above, but faster. It’s simpler because all objects
have wait() and notify() built in.

The synchronized key word causes threads to pause and wait for consistent data
without any method calls. Methods and code blocks can be synchronized. A
synchronized method locks its own object (this) before it proceeds, manipulates
the shared data and releases the lock when it returns. (A static method locks
the Class object.) For a synchronized code block, the object to be locked must
be explicitly identified.
 
Ashuthosh san
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

The information is very useful, Thanks!. Could you please share any example for these methods. I am bit confused how to use these methods.

Regards
San
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, and thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic