Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes Thread with synchronized run() method and sleep() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread with synchronized run() method and sleep()" Watch "Thread with synchronized run() method and sleep()" New topic
Author

Thread with synchronized run() method and sleep()

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
This code starts two parallel threads which print out:
hread-1i=576
Thread-1i=577
Thread-1i=578
Thread-1i=579
Thread-2i=0
Thread-2i=1
Thread-2i=2
Thread-2i=3
... until every thread has reached 1000.


First question:
- What is the effect of „synchronized“ in the run() method?
The threads are running parallel despite of „synchronized“. I thought „synchronized“
would lock the run method for one Thread so that the threads don’t run parallel but sequential.
- What will happen if I inserted a „yield()“, try{sleep(1000);}catch (InterruptedException ie){} or
wait(1000) into the run() method?

Now the result is that the threads b1 and b2 run more parallel than before. In the first program the CPU
Changed threads on average on 500 loops. In the program with yield() the threads change on every loop.
- Is the result of the yield()that the two threads run better parallel than before???
- Did the two threads run parallel before in the first programm (no yield()) ???

C:\Java\EigeneJavaProgramme>java BussyThread
Thread-1i=0
Thread-2i=0
Thread-1i=1
Thread-2i=1
Thread-1i=2
Thread-2i=2
Thread-1i=3
Thread-2i=3
Thread-1i=4
Thread-2i=4
......
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
Hi Thomas,
I am trying to answer some of your questions and hope I am not misleading. If any virtuosos on java threads here see any misconceptions, please point out. Thanks in advance.

What is the effect of �synchronized� in the run() method?
"synchronized" is to prevent more than one threads accessing a same object concurrently.
I thought �synchronized� would lock the run method for one Thread so that the threads don�t run parallel but sequential
Yes. Just think it this way: the purpose you put a "synchronized" modifier is to tell the JVM: Hi JVM, the following method/block might be accessed by more than one threads concurrently, but I don�t want this happen. I need your help, please make the accesses sequential for me. So that the JVM "locks" the object.
When yield() is called, the current running thread releases the CPU resource which it is using, and queued, waits for its next turn to possess some CPU cycles. So that its status is "runnable" after a yield() call. It�s interrupted but not terminated.
wait(1000)
No, this is not a proper way to use wait(). wait() has no parameter. You wait() for some notify/notifyAll, but you cannot specify how long. JVM controls it completely. And the wait() method is final in the Object class, you cannot overwrite it.
Is the result of the yield()that the two threads run better parallel than before???
Not really concurrently. you�d rather say: The execution of the two threads was cut into smaller/more pieces than before. In every turn, each threads gets some CPU cycles for small pieces, of course it appears more "concurrently". Actually in a single-CPU box, your two threads are always executed sequentially, never concurrently.

That�s what I can tell.
Here I don�t understand:
hread-1i=576
Thread-1i=577
Thread-1i=578
Thread-1i=579
Thread-2i=0
Thread-2i=1
Thread-2i=2
Thread-2i=3
since the run() is synchronized, why the Thread-1 only reached 579( I think should be 1000) when it passed the control to thread2?? Anyone be so kind and explain? Thank you in advance!

Regards,
Ellen
[ January 17, 2003: Message edited by: Ellen Fu ]
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937


No, this is not a proper way to use wait(). wait() has no parameter. You wait() for some notify/notifyAll, but you cannot specify how long. JVM controls it completely.


This is wrong info. I use wait(timeout) all the time. Here is javadoc for this method in Suns API:
public final void wait(long timeout)
throws InterruptedException
Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a secified amount of time has elapsed.


The threads are running parallel despite of �synchronized�. I thought �synchronized�
would lock the run method for one Thread so that the threads don�t run parallel but sequential.


This is what many people miss. If a method foo() of object A is synchronized, then two threads calling method foo() on object A will indeed be sequential. However, in your case, you create two objects. Consequently, while the first object runs its synchronized method, the second object doesn't have to wait at all.
Eugene.
[ January 18, 2003: Message edited by: Eugene Kononov ]
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
Originally posted by Eugene Kononov:

This is wrong info. I use wait(timeout) all the time. Here is javadoc for this method in Suns API:
public final void wait(long timeout)
throws InterruptedException
Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a secified amount of time has elapsed.
...
This is what many people miss. If a method foo() of object A is synchronized, then two threads calling method foo() on object A will indeed be sequential. However, in your case, you create two objects. Consequently, while the first object runs its synchronized method, the second object doesn't have to wait at all.

Thank you very much Eugene! I�m happy you corrected my severe mistake and also cleared up some of my misconception.

Regards,
Ellen
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
There is even a
public final void wait(long timeout,
int nanos)
throws InterruptedException
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Thomas, synchronize works on "objects." So that means only 1 thread may access a given synchronized method of a specific object at any one time.
But you have created 2 different objects and as a result are synchronizing on 2 differen things. therefore you are never blocked because thread 1 is only accessing the run method of object b1, and thread 2 is only accessing the run method of object b2.
Try this. put a static object there that they both can access and synchronize on that object. then see what happens.

now both threads will synchronize on the same object. Run that and see what happens.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread with synchronized run() method and sleep()