public class ThreadTestSyncMethod {
public static void main(String[] args) {
final ThreadTestSyncMethod t = new ThreadTestSyncMethod();
for (int i = 0; i < 5; i++) {
new Thread() {
@Override
public void run() {
super.run();
t.test1();
}
}.start();
new Thread() {
@Override
public void run() {
super.run();
t.test2();
}
}.start();
}
}
public synchronized void test1() {
System.out.print("test1 start\n");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("test1 end\n");
}
public synchronized void test2() {
System.out.print("test2 start\n");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("test2 end\n");
}
}
test1 start
test1 end
test2 start
test2 end
test1 start
test1 end
test2 start
test2 end
test1 start
test1 end
test2 start
test2 end
test1 start
test1 end
test2 start
test2 end
test1 start
test1 end
test2 start
test2 end
Process finished with exit code 0