51,396
社区成员




public class Test {
V v = new V();
public void go() {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
v.f1();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
v.f2();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
v.f3();
}
}
}).start();
}
public static void main(String[] args) {
Test t = new Test();
t.go();
}
class V {
public synchronized void f1() {
System.out.println("method1");
}
public synchronized void f2() {
System.out.println("method2");
}
public synchronized void f3() {
System.out.println("method3");
}
}
}
public synchronized void f1() {
System.out.println("method1");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}