Forums Register Login

syncronizing run() method

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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 means our mission is in jeapordy! Quick, read this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1152 times.
Similar Threads
synchronization not working properly
Thread Question
Thread Doubt
Threading synchronization
Problem in thread
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:54:03.