class CommonHelloWorld22222222222222 {
public synchronized void synchronizedMethod1() {
System.out.println ("synchronizedMethod1 called");
try {
Thread.sleep (1000);
} catch (InterruptedException e) {
e.printStackTrace ();
}
System.out.println ("synchronizedMethod1 done");
}
public synchronized void synchronizedMethod2() {
System.out.println ("synchronizedMethod2 called");
try {
Thread.sleep (1000);
} catch (InterruptedException e) {
e.printStackTrace ();
}
System.out.println ("synchronizedMethod2 done");
}
}
public class SynchronizedMethod12 extends Thread {
private int id = 0;
private Common common;
public SynchronizedMethod12 (String name, int no, Common object) {
super(name);
common = object;
id = no;
}
public void run () {
System.out.println ("Running Thread" + this.getName ());
try {
if (id == 0) {
common.synchronizedMethod1();
} else {
common.synchronizedMethod2();
}
} catch (Exception e) {
e.printStackTrace ();
}
}
public static void main (String[] args) {
Common c = new Common ();
SynchronizedMethod12 t1 = new SynchronizedMethod12 ("SynchronizedMethod12-1", 0, c);
SynchronizedMethod12 t2 = new SynchronizedMethod12 ("SynchronizedMethod12-2", 1, c);
t1.start ();
t2.start ();
}
}
测试
最新推荐文章于 2023-03-11 18:36:24 发布