使用 synchronized(obj) 和声明的锁之间的区别
Differences between using synchronized(obj) and a declared lock
我正在尝试同步此 运行() 方法。我希望一次只有一个线程进入其中,但我不明白为什么解决方案告诉我使用 synchronized(obj) 方法:
public Tabellina(Contatore o, int num) {
obj = o;
numero = num;
r = new Random();
start();
}
public void run() {
synchronized(obj) {
for (int j=0; j<10; j++) {
obj.incr();
try {
Thread.sleep(100);
} catch (InterruptedException e) { }
System.out.println(Thread.currentThread().getName() + ": " + obj.getVal());
}
try {
Thread.sleep(r.nextInt(2000));
} catch (InterruptedException e) { }
}}
}
我的想法是创建一个锁并以这种方式同步它:
public Tabellina(Contatore o, int num) {
obj = o;
numero = num;
r = new Random();
l1 = new ReentrantLock();
start();
}
public synchronized void run() {
l1.lock();
for (int j=0; j<10; j++) {
obj.incr();
try {
Thread.sleep(100);
} catch (InterruptedException e) { }
System.out.println(Thread.currentThread().getName() + ": " + obj.getVal());
}
try {
Thread.sleep(r.nextInt(2000));
} catch (InterruptedException e) { }
l1.unlock();
}
}
但它不起作用,因为线程交替打印 for 的内容。这怎么可能?如果我在 运行 方法的开头使用 lock() ,难道第一个线程不应该完成 for 然后释放锁并让第二个线程进入该方法吗?
正在使用
public synchronized void run() {
是
的简写
public void run() {
synchronized (this) {
由于this
每个对象的锁各不相同,所以它们是相互独立的。换句话说,它对你的情况没有任何作用。
我正在尝试同步此 运行() 方法。我希望一次只有一个线程进入其中,但我不明白为什么解决方案告诉我使用 synchronized(obj) 方法:
public Tabellina(Contatore o, int num) {
obj = o;
numero = num;
r = new Random();
start();
}
public void run() {
synchronized(obj) {
for (int j=0; j<10; j++) {
obj.incr();
try {
Thread.sleep(100);
} catch (InterruptedException e) { }
System.out.println(Thread.currentThread().getName() + ": " + obj.getVal());
}
try {
Thread.sleep(r.nextInt(2000));
} catch (InterruptedException e) { }
}}
}
我的想法是创建一个锁并以这种方式同步它:
public Tabellina(Contatore o, int num) {
obj = o;
numero = num;
r = new Random();
l1 = new ReentrantLock();
start();
}
public synchronized void run() {
l1.lock();
for (int j=0; j<10; j++) {
obj.incr();
try {
Thread.sleep(100);
} catch (InterruptedException e) { }
System.out.println(Thread.currentThread().getName() + ": " + obj.getVal());
}
try {
Thread.sleep(r.nextInt(2000));
} catch (InterruptedException e) { }
l1.unlock();
}
}
但它不起作用,因为线程交替打印 for 的内容。这怎么可能?如果我在 运行 方法的开头使用 lock() ,难道第一个线程不应该完成 for 然后释放锁并让第二个线程进入该方法吗?
正在使用
public synchronized void run() {
是
的简写public void run() {
synchronized (this) {
由于this
每个对象的锁各不相同,所以它们是相互独立的。换句话说,它对你的情况没有任何作用。