• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

syncronizing run() method

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

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
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic