Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes syncronizing run() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "syncronizing run() method" Watch "syncronizing run() method" New topic
Author

syncronizing run() method

Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
What are the implications of syncronizing the run() methods in creation of Threads by extending a Thread class.

and implementing Runnable


Now the quesion is: Is there any effect occurs of syncronizing the run()in either case.Does the multithreading execution will not take place?Does it works like a serial execution of the statements?
[ November 16, 2006: Message edited by: Sanjeev Kumar Singh ]

~Sanjeev Singh<br />SCJP 1.5
Maneessh saxena
Ranch Hand

Joined: Oct 03, 2006
Posts: 125
hi sanjeev,

//code number 1

class MyThread extends Thread{
public MyThread(String name){
super(name);
}
public synchronized void run(){
for(int i = 0 ; i<50 ;i++)
System.out.println(Thread.currentThread().getName());
try{Thread.sleep(10);}catch(Exception e){}
}
public static void main(String [] args){
new MyThread ("one").start();//Thread number one
new MyThread ("two").start(); //Thread number two
new MyThread ("three").start(); //Thread number three
}
}


//code number 2

class MyThread implements Runnable{
public synchronized void run(){
for(int i = 0 ; i<50 ;i++)
System.out.println(Thread.currentThread().getName());
try{Thread.sleep(10);}catch(Exception e){}
}
public static void main(String [] args){
MyThread td = new MyThread();
Thread t1 = new Thread(td ,"One" );
Thread t2 = new Thread(td ,"two" );
Thread t3 = new Thread(td ,"three" );
t1.start();
t2.start();
t3.start();
}
}


Once a Thread acquires lock of an object (by entering synchronized method of that object) no other Thread can call any synchronized method of that class on that object .

in code number 1 three Thread objects are created and hence they access run() method randomly even if it’s synchronized . reason being these are three different Threads.


in code number 2 three Thread objects are created and given the the same Runnable but as method run() is synchronized once a Thread acquires lock no other Thread can call any synchronized method of that class on that object so second hread will enter to run() method only if first Thread entered to run() leves the lock acquired.

Serial execution of statements here not guaretees any execution ordered.it is random all the time.JVM may choose any of the Thread to be currently running Thread at any time until unless priorities are not given and JVM also supports priorities.


It wil be more clear you please compile and run the codes that I written.

If still any confusion please let me know..

Regards


SCJP 1.4, SCWCD 1.5
Saurabh Vyas
Ranch Hand

Joined: Sep 02, 2003
Posts: 72
If we synchronize run method in a class that extends Thread than it will not have much effect. Since every time we create an object of that class, each object will have its own run method which will be used by that object only. Thus no synchronization comes into picture.

Following code explains this :



But if we synchronize run method in a class that implements Runnable than the synchronization comes into picture. This is because we create a single object of the class that implements Runnable and than use this object to create more than one threads. Thus each thread would be using a common run method. So in this case synchronization becomes important.

check out the following code :


Try to remove synchronized keywork from run method in above code to see the difference.

Regards
Saurabh
Maneessh saxena
Ranch Hand

Joined: Oct 03, 2006
Posts: 125
Originally posted by Saurabh Vyas:

[/CODE]


Try to remove synchronized keywork from run method in above code to see the difference.

Regards
Saurabh


Hi saurabh,

I agree with you and I’ve mentioned in my explanation like Thread number 1, Thread number 2 ,Thread number 3.all are different objects of class that extends Thread. and run independendtly of each other irrespective of synchronization run( ).

But in Runnable implementation all Threads have same Runnable and calls method in synchronization .Other Thread may call synchronized method at the same time on other Runnable objects but not on the object whose Lock is acquired previously by another Thread .

Hope it clears all doubts.

Regards
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Raunak and Saurabh,
Thanks for a very good reply.The things which I concluded from the disscussion is that since each thread created by Extending Thread(class) will have their own run method so there will be no effect of syncronizing the method run(),means no race condition.While in the case of target created by implementing Runnable interface and various thread haveing the same target will have same run() method to run,so syncronzation takes place in this case.

Also I have a question,Can we conclude that the threads having common runnable target can syncronize on this object while threads exteing Thread(class) can not?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: syncronizing run() method