Hy, i try to follow this http://www.studytonight.com/java/synchronization.php
here's my code
class First {
public void display(String msg)
{
System.out.print("["+msg);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}
System.out.println("]");
}
}
class Second extends Thread{
String msg;
First fobj;
Second(First fp,String str){
msg=str;
fobj=fp;
start();
}
public void run(){
synchronized(fobj){
fobj.display(msg);
}
}
}
public class Main {
public static void main(String[] args) {
// TODO code application logic here
First f=new First();
Second s1=new Second(f,"welcome");
Second s2=new Second(f,"new");
Second s3=new Second(f,"programmer");
}
}
and here's my result
run:
[welcome]
[programmer]
[new]
BUILD SUCCESSFUL (total time: 3 seconds)
what's wrong with my code? why the result isn't welcome new programmer ?