• 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

Thread with synchronized run() method and sleep()

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
......
 
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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

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
Posts: 581
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is even a
public final void wait(long timeout,
int nanos)
throws InterruptedException
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic