• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Synchronization

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Observe the following code
public class Base extends Thread{

public static void main(String []args){

Base b1 = new Base();
Base b2 = new Base();
b1.start();
b2.start();


}
public synchronized void run(){
System.out.println(this.toString()+"1");
for(int i=0;i<1000;i++);
System.out.println(this.toString()+"2");
}
}
// This was run on Win 95
// The output at times is mixed i.e. The synchronization is not allowing a single thread to run until it finishes its work and then the next thread to up the run method.
Is it due to the OS
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't how it can produce different results.
Your for loop does nothing since you put the ; after the loop.
The output should be consistent:
Thread[Thread-0,5,main]1
Thread[Thread-0,5,main]2
Thread[Thread-1,5,main]1
Thread[Thread-1,5,main]2
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The synchronization in the example locks the Base object (Base1 in mine below). So, there is a lock on object b1 and a lock on b2. The lock on b1 does not prevent synchronized methods from running on object referenced by b2 (run() in this example).
We're mixing concepts here and now I have a doubt about what I was going to assert. I added a sleep() call to the for loop. This allows other threads execution time, but it doesn't give up the lock like wait()does, does it?? I don't think so.
<PRE>
public class Base1 extends Thread{
public static void main(String []args){
Base1 b1 = new Base1();
Base1 b2 = new Base1();
System.out.println("Start b1");
b1.start();
System.out.println("Start b2");
b2.start();
}
public synchronized void run(){
System.out.println(this.toString()+"1");
for(int i=0;i<100;i++) {<br /> System.out.println(this.toString()+"2");<br /> try {<br /> sleep(50);<br /> } catch (InterruptedException e) {}<br /> }<br /> }<br /> }<br /> </PRE><br /> output looks like this:<br /> D:\java\jr>java Base1
Start b1
Start b2
Thread[Thread-0,5,main]1
Thread[Thread-0,5,main]2
Thread[Thread-1,5,main]1
Thread[Thread-1,5,main]2
Thread[Thread-0,5,main]2
Thread[Thread-1,5,main]2
Thread[Thread-0,5,main]2
Thread[Thread-1,5,main]2
Thread[Thread-0,5,main]2
...
[This message has been edited by Carol Enderlin (edited December 01, 2000).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each thread (b1 and b2) is a different object and hence an unique instance of class Base. Each object has its own copy of the run method which means, the synchronization here really has no effect because after all, there is only one thread that is executing the method.
Carol, you are right. Calling sleep() does not make the thread to relinquish the lock. When a thread is sleeping, it is still consuming CPU cycles and theoretically it is in the 'running state' and waiting threads continue to wait.
Ajith
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ajith,
One text I have, Concurrent Programming in Java says that sleep() is implemented as a timed wait().
Wouldn't that mean sleep() does give up the lock as it actually enters a wait() state?

Hi ... I want to correct this post ... turns out I misread the book I quoted; it does state "The Thread.sleep(long msecs) method uses a timed wait, but does not tie up the current object's synchronization lock."
Don't want to malign the authors

------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
[This message has been edited by Jane Griscti (edited December 02, 2000).]
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane,
Here's what Java API documentation says for the sleep() method


sleep
public static void sleep(long millis)
throws InterruptedException
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.
Parameters:
millis - the length of time to sleep in milliseconds.
Throws:
InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
See Also:
Object.notify()


Actually when a thread goes to sleep, if it is not owning any locks, then it behaves like the yield method ie., it allows other threads ( in the waiting pool ) to run. Infact the wait method is internally implemented using using the sleep().
On the otherhand, if the sleep is called in a synchronized method, the thread owns the monitor and hence it will not relinquish the lock. The scheduler can pickup other threads( in the waiting pool ) and give them a chance to run, BUT any other thread cannot execute the synchronized method.... and you know why
An excellent "expret" article about Threads by OReilly is worth its weight in gold. You may want to check it out !
Hope I have cleared the confusion
Ajith
[This message has been edited by Ajith Kallambella (edited December 01, 2000).]
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a synchronization to allow a single thread to run then try it
public class Base1 extends Thread{
public static void main(String []args){
Base1 b=new Base1();
Thread t1=new Thread(b);
Thread t2=new Thread(b);
System.out.println("Start b1");
t1.start();
System.out.println("Start b2");
t2.start();
}
public synchronized void run(){
System.out.println(this.toString()+"1");
for(int i=0;i<100;i++) {
System.out.println(this.toString()+"2");
try {
sleep(50);
} catch (InterruptedException e) {}
}
}
}
Now there is only one lock with b and t1 and t2 are using it.
Ajith,
You said that yeild() method causes the thread to loses the ownership of lock but that is not the case.Even there's not use
of using of yeild() method in a synchronized method.......
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nasir,
I think you read me wrong. Please read the text again. I only said


Actually when a thread goes to sleep, if it is not owning any locks, then it behaves like the yield method ie., it allows other threads ( in the waiting pool ) to run.


Ajith
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ajith. I'll check out the link.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes,I read you wrong Ajit,
But is it true whatever I said about yeild method that there's no use of using of yeild() method in a synchronized method?
Doc. says yield() causes the currently executing thread object to temporarily pause and allow other threads to execute.
'pause' means it should not release the lock if used in a synchronized method.Am I right.
Thanks.
 
It's feeding time! Give me the food you were going to give to this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!