Hello your program above "class Share extends Thread" doesn't have any thread synchronization effect because lock is placed on the different object of the class. To make it work properly you need to make dispay() method static.
Please make correction in that.
It very interesting and usefull theme. But I think that we can synchronized methods as callback function. In this situation we can have mutex object and semaphore with lock. One method lock semaphore after that another method can read it and synchronized with first metod regardless of the time of his call. It may be usefull if we work with network or with great resource loading and don't knew delay time.
It's true?
I am not sure whether the above example (with synchronization block) have been executed or not. But, I tried executing them. First thing, it does not compile because of missing braces. Once i fixed that issue, it does not give desired result as posted, instead it prints randomly every time you execute but definitely not gives single time the posted output. And i think that was not the objective of this article. The last output that i got, am pasting it here:
Thread One: This
Thread Two: This
Thread One: is
Thread Two: is
Thread One: a
Thread Two: a
Thread Two: synchronized
Thread One: synchronized
Thread One: variable
Thread Two: variable
Hope the author reads my post and post corrections.
Tried running your provided code and neither examples produced syncd output. Besides, there is a bug in the second example. One curly bracket is missing.
Thread One: This
Thread One: is
Thread One: a
Thread Two: This
Thread Two: is
Thread Two: a
Thread Two: synchronized
Thread Two: variable
Thread One: synchronized
Thread One: variable
Your display() method should also be static in order for the synchronized keyword to make any difference.
Also I would suggest this code for a better understanding of the synchronized keyword :
class Shared {
public String msg[]={"This", "is", "a", "synchronized", "variable"};
public synchronized void display(String threadN){
for(int i=0;i<=4;i++){
System.out.println(threadN+msg[i]);
try{
Thread.currentThread().sleep(1000);
}catch(Exception e){
System.out.println("ERROR !");
}
}
}
}
class ShareThread extends Thread{
private Shared shared;
ShareThread(String threadname, Shared shared){
super(threadname);
this.shared = shared;
}
public void run(){
shared.display(getName());
}
}
public class NewClass {
public static void main(String[] args) {
Shared shared = new Shared();
ShareThread t1=new ShareThread("Thread One: ",shared);
t1.start();
ShareThread t2=new ShareThread("Thread Two: ",shared);
t2.start();
}
}
threadsmukesh July 1, 2011 at 11:09 PM
hi explain me 'Share(String threadname)' this line
Wrong program "class Share extends Thread"Avinash December 16, 2011 at 11:19 PM
Hello your program above "class Share extends Thread" doesn't have any thread synchronization effect because lock is placed on the different object of the class. To make it work properly you need to make dispay() method static. Please make correction in that.
synchronized methodsjurijs January 7, 2012 at 10:09 PM
It very interesting and usefull theme. But I think that we can synchronized methods as callback function. In this situation we can have mutex object and semaphore with lock. One method lock semaphore after that another method can read it and synchronized with first metod regardless of the time of his call. It may be usefull if we work with network or with great resource loading and don't knew delay time. It's true?
Output Not as mentionedCD February 15, 2012 at 3:00 AM
I am not sure whether the above example (with synchronization block) have been executed or not. But, I tried executing them. First thing, it does not compile because of missing braces. Once i fixed that issue, it does not give desired result as posted, instead it prints randomly every time you execute but definitely not gives single time the posted output. And i think that was not the objective of this article. The last output that i got, am pasting it here: Thread One: This Thread Two: This Thread One: is Thread Two: is Thread One: a Thread Two: a Thread Two: synchronized Thread One: synchronized Thread One: variable Thread Two: variable Hope the author reads my post and post corrections.
about javabalaraju March 1, 2012 at 11:19 PM
briefly gives the matter
ThreadingSushant April 16, 2012 at 5:49 PM
In the above example "public void display(String threadN)", why threadN is passed to display? What is the use of that?
javaRaveesh Kumar April 28, 2012 at 1:37 AM
This program is not synchronized
sync thread examples Andy May 9, 2012 at 3:10 AM
Tried running your provided code and neither examples produced syncd output. Besides, there is a bug in the second example. One curly bracket is missing. Thread One: This Thread One: is Thread One: a Thread Two: This Thread Two: is Thread Two: a Thread Two: synchronized Thread Two: variable Thread One: synchronized Thread One: variable
venkataramanvenkataraman June 29, 2012 at 3:30 AM
good
Java monitor is not a "semaphore" Seshadri Rangaswami December 17, 2012 at 3:58 AM
Java monitor is a "mutex" and not a "semaphore". Of course, mutex is a degenerated version of Semphore with only one lock.
syncronized threadankur November 1, 2012 at 11:58 AM
imp
Wrong use of synchronizeAndrei April 17, 2013 at 3:42 PM
Your display() method should also be static in order for the synchronized keyword to make any difference. Also I would suggest this code for a better understanding of the synchronized keyword : class Shared { public String msg[]={"This", "is", "a", "synchronized", "variable"}; public synchronized void display(String threadN){ for(int i=0;i<=4;i++){ System.out.println(threadN+msg[i]); try{ Thread.currentThread().sleep(1000); }catch(Exception e){ System.out.println("ERROR !"); } } } } class ShareThread extends Thread{ private Shared shared; ShareThread(String threadname, Shared shared){ super(threadname); this.shared = shared; } public void run(){ shared.display(getName()); } } public class NewClass { public static void main(String[] args) { Shared shared = new Shared(); ShareThread t1=new ShareThread("Thread One: ",shared); t1.start(); ShareThread t2=new ShareThread("Thread Two: ",shared); t2.start(); } }
Post your Comment