Java中同步语句的静态锁对象
请不要陷入这样的陷阱:你可以锁定一个任意的类A实例,而这会以某种方式锁定另一个类A实例。这是一个典型的初学者错误 我犯了几次错误才明白。但是静态锁对象工作正常 神话阅读Java中同步语句的静态锁对象,java,multithreading,synchronization,Java,Multithreading,Synchronization,请不要陷入这样的陷阱:你可以锁定一个任意的类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
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;
}
}
}
}
}
输出
test_Method Thread-0
Leaving test_Method on Thread-0
test_Method Thread-1
Leaving test_Method on Thread-1
带有非静态锁定对象的输出(不适合我)
使用
静态
锁定对象是否是一个好主意?而不是静态让MyThread共享一个锁定对象会更好。更面向对象。与静态相比,让MyThread共享一个锁对象更好。更面向对象。您可以锁定类本身-这更有意义且更易于阅读:
private void test_Method() throws InterruptedException {
synchronized (MyThread.class) {
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 in " + Thread.currentThread().getName());
return;
}
}
}
}
private void test_Method()抛出InterruptedException{
已同步(MyThread.class){
System.out.println(“test_方法”+Thread.currentThread().getName());
对于(int i=0;i
或者,如果不需要将该方法作为实例方法:
private static synchronized void test_Method() throws InterruptedException {
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 in " + Thread.currentThread().getName());
return;
}
}
}
private static synchronized void test_Method()抛出InterruptedException{
System.out.println(“test_方法”+Thread.currentThread().getName());
对于(int i=0;i
您可能还需要阅读新的(ish)类。您可以锁定类本身-这更有意义且更易于阅读:
private void test_Method() throws InterruptedException {
synchronized (MyThread.class) {
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 in " + Thread.currentThread().getName());
return;
}
}
}
}
private void test_Method()抛出InterruptedException{
已同步(MyThread.class){
System.out.println(“test_方法”+Thread.currentThread().getName());
对于(int i=0;i
或者,如果不需要将该方法作为实例方法:
private static synchronized void test_Method() throws InterruptedException {
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 in " + Thread.currentThread().getName());
return;
}
}
}
private static synchronized void test_Method()抛出InterruptedException{
System.out.println(“test_方法”+Thread.currentThread().getName());
对于(int i=0;i
您可能还需要阅读新的(ish)类。它是静态成员还是实例成员取决于您希望它具有的范围,但肯定是什么一个好的做法是有一个私有对象来锁定。这是锁定(显然是公共的)类对象的主要优势。它是静态成员还是实例成员取决于您希望它拥有的范围,但肯定的是,一个好的做法是锁定一个私有对象。这是锁定(显然是公共的)类对象的主要优势。让一个静态对象和两个线程对其进行修改对我来说没有多大意义,为什么不在一个线程中完成这项工作?@BobPlannigon,我发布的代码是我实际应用程序中非常简化的一段代码。我有很多线程同时工作,必须处理部分代码,只允许一个线程进入。让一个静态对象和两个线程修改它对我来说没有多大意义,为什么不在一个线程中完成工作?@BobPlannigon,我发布的代码是我真实应用程序中非常简化的一段代码。我有很多线程同时工作,必须处理部分代码,只允许一个线程进入。