一.锁代码块
1.加static
class Sync2 {
public static synchronized void test() {
System.out.println("test开始..");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("test结束..");
}
}
class MyThread2 extends Thread {
public void run() {
Sync2 sync = new Sync2();
sync.test();
}
}
public class Main2 {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
Thread thread = new MyThread2();
thread.start();
}
}
}
运行结果:
2.不加static
class Sync2 {
public