并且请不要陷入认为您可以锁定 A 类的任意实例并且会以某种方式锁定 A 类的另一个实例的陷阱。这是一个经典的初学者错误。
在我理解它之前,我犯了几次错误。但是静态锁对象可以正常工作。
我的线程
package com.replanet;
public class MyThread extends Thread {
private int x, y;
private static Object lock3 = new Object();
public MyThread(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void run() {
super.run();
try {
test_Method();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void test_Method() throws InterruptedException {
synchronized (lock3) {
System.out.println("test_Method " + Thread.currentThread().getName());
for (int i = 0; i < Integer.MAX_VALUE; i++) {
if (i == Integer.MAX_VALUE / 2) {
Thread.sleep(2000);
System.out
.println("Leaving test_Method on " + Thread.currentThread().getName());
return;
}
}
}
}
}
用法
package com.replanet;
public class Main {
public static void main(String[] args) {
MyThread myThread1 = new MyThread(1, 2);
MyThread myThread2 = new MyThread(1, 2);
myThread1.start();
myThread2.start();
}
}
输出
test_Method Thread-0
Leaving test_Method on Thread-0
test_Method Thread-1
Leaving test_Method on Thread-1
带有非静态锁定对象的输出(不适合我)
test_Method Thread-0
test_Method Thread-1
Leaving test_Method on Thread-1
Leaving test_Method on Thread-0
static
使用锁定对象是个好主意吗?