In Programming Interviews Exposed book (Wrox publications), code for Producer consumer problem uses 'synchronized' keyword for each of produce() and consume() methods inside a class called IntBuffer. Is this different than using synchronized(this) inside each of those methods ? The book says, "When a thread is busy waiting in produce(), no thread can enter consume() because methods are synchronized." I don't feel that makes sense for the code in the book because, when a thread is busy waiting in produce(), no thread can enter produce(). However other thread can enter consume() which shatters the idea of mutual exclusion. The methods produce and consume should both entirely be synchronized right ?
在编程访谈中暴露的书籍(Wrox出版物),生产者消费者问题的代码对名为IntBuffer的类中的每个produce()和consume()方法使用'synchronized'关键字。这与在每个方法中使用synchronized(this)不同吗?该书说,“当一个线程忙于在produce()中等待时,没有线程可以进入consume(),因为方法是同步的。”我觉得这对书中的代码没有意义,因为当一个线程忙于在produce()中等待时,没有线程可以进入produce()。然而,其他线程可以进入consume(),这破坏了互斥的想法。产生和消费的方法应该完全同步吗?
Code in the book:
书中的代码:
public class IntBuffer
{
private int index;
private int[] buffer = new int[8];
// Function called by producer thread
public synchronized void produce(int num) {
while(index == buffer.length - 1) {
try { wait();}
catch(InterruptedException ex) {}
}
buffer[index++] = num;
notifyAll();
}
// Function called by consumer thread
public synchronized int consume() {
while(index == 0) {
try { wait();}
catch(InterruptedException ex) {}
}
int ret = buffer[--index];
notifyAll();
return ret;
}
}
4 个解决方案
#1
2
No, they are the same.
不,他们是一样的。
private synchronized void foo() {}
private void foo2() {
synchronized(this){
}
}
They will do the exact same as both monitor the instance which they are called from.
它们将完全相同,因为它们都监视它们被调用的实例。
A good tutorial can be found in the blog of Jakob Jenkov http://tutorials.jenkov.com/java-concurrency/synchronized.html#java-synchronized-example
可以在Jakob Jenkov的博客中找到一个好的教程http://tutorials.jenkov.com/java-concurrency/synchronized.html#java-synchronized-example
Happy coding!
#2
0
Q: Is this different than using synchronized(this) inside each of those methods ? A : No, it is not different, by using block (synchronize(this) you can synchronize just part of your code, not the whole method. For example :
问:这与在每个方法中使用synchronized(this)不同吗?答:不,它没有什么不同,通过使用块(同步(this)你可以同步部分代码,而不是整个方法。例如:
public void m1(){
// some code
synchronized(this){
// thread-safe code
}
#3
0
Is this different than using synchronized(this) inside each of those methods?
这与在每个方法中使用synchronized(this)不同吗?
No.
The book says, "When a thread is busy waiting in produce(), no thread can enter consume() because methods are synchronized." I don't feel that makes sense for the code in the book because, when a thread is busy waiting in produce(), no thread can enter produce(). However other thread can enter consume() which shatters the idea of mutual exclusion.
该书说,“当一个线程忙于在produce()中等待时,没有线程可以进入consume(),因为方法是同步的。”我觉得这对书中的代码没有意义,因为当一个线程忙于在produce()中等待时,没有线程可以进入produce()。然而,其他线程可以进入consume(),这破坏了互斥的想法。
That's not correct. Both methods are synchronized on the same object, so only one thread can be in either method, unless wait()
is called, which it is, which releases the lock.
那不对。两个方法都在同一个对象上同步,因此只有一个线程可以在任一个方法中,除非调用wait(),它会释放锁。
The methods produce and consume should both entirely be synchronized right?
产生和消费的方法应该完全同步吗?
Yes, and you said they are. Unclear what you are asking here.
是的,你说他们是。不清楚你在这里问什么。
#4
0
Using synchronized(this) requires the calling thread to take the same lock as when it calls an instance method using the synchronized modifier on the method. There are some differences in what bytecode is generated but that's a pretty low-level distinction.
使用synchronized(this)要求调用线程采用与使用方法上的synchronized修饰符调用实例方法时相同的锁定。生成什么字节码存在一些差异,但这是一个非常低级别的区别。
The purpose of the synchronized keyword is to protect shared state from concurrent access. The produce and consume methods use the same internal state so it's reasonable that they are both protected by the same lock.
synchronized关键字的目的是保护共享状态不受并发访问的影响。产品和消费方法使用相同的内部状态,因此它们都受同一个锁保护是合理的。
The posted code looks well-done, my only nitpick is i would let the methods throw InterruptedException instead of catching it. Both the produce and consume methods require the calling thread to acquire the lock on the instance that the method is being called on.
发布的代码看起来做得很好,我唯一的挑剔是我会让方法抛出InterruptedException而不是捕获它。产生和使用方法都需要调用线程获取正在调用该方法的实例上的锁。
#1
2
No, they are the same.
不,他们是一样的。
private synchronized void foo() {}
private void foo2() {
synchronized(this){
}
}
They will do the exact same as both monitor the instance which they are called from.
它们将完全相同,因为它们都监视它们被调用的实例。
A good tutorial can be found in the blog of Jakob Jenkov http://tutorials.jenkov.com/java-concurrency/synchronized.html#java-synchronized-example
可以在Jakob Jenkov的博客中找到一个好的教程http://tutorials.jenkov.com/java-concurrency/synchronized.html#java-synchronized-example
Happy coding!
#2
0
Q: Is this different than using synchronized(this) inside each of those methods ? A : No, it is not different, by using block (synchronize(this) you can synchronize just part of your code, not the whole method. For example :
问:这与在每个方法中使用synchronized(this)不同吗?答:不,它没有什么不同,通过使用块(同步(this)你可以同步部分代码,而不是整个方法。例如:
public void m1(){
// some code
synchronized(this){
// thread-safe code
}
#3
0
Is this different than using synchronized(this) inside each of those methods?
这与在每个方法中使用synchronized(this)不同吗?
No.
The book says, "When a thread is busy waiting in produce(), no thread can enter consume() because methods are synchronized." I don't feel that makes sense for the code in the book because, when a thread is busy waiting in produce(), no thread can enter produce(). However other thread can enter consume() which shatters the idea of mutual exclusion.
该书说,“当一个线程忙于在produce()中等待时,没有线程可以进入consume(),因为方法是同步的。”我觉得这对书中的代码没有意义,因为当一个线程忙于在produce()中等待时,没有线程可以进入produce()。然而,其他线程可以进入consume(),这破坏了互斥的想法。
That's not correct. Both methods are synchronized on the same object, so only one thread can be in either method, unless wait()
is called, which it is, which releases the lock.
那不对。两个方法都在同一个对象上同步,因此只有一个线程可以在任一个方法中,除非调用wait(),它会释放锁。
The methods produce and consume should both entirely be synchronized right?
产生和消费的方法应该完全同步吗?
Yes, and you said they are. Unclear what you are asking here.
是的,你说他们是。不清楚你在这里问什么。
#4
0
Using synchronized(this) requires the calling thread to take the same lock as when it calls an instance method using the synchronized modifier on the method. There are some differences in what bytecode is generated but that's a pretty low-level distinction.
使用synchronized(this)要求调用线程采用与使用方法上的synchronized修饰符调用实例方法时相同的锁定。生成什么字节码存在一些差异,但这是一个非常低级别的区别。
The purpose of the synchronized keyword is to protect shared state from concurrent access. The produce and consume methods use the same internal state so it's reasonable that they are both protected by the same lock.
synchronized关键字的目的是保护共享状态不受并发访问的影响。产品和消费方法使用相同的内部状态,因此它们都受同一个锁保护是合理的。
The posted code looks well-done, my only nitpick is i would let the methods throw InterruptedException instead of catching it. Both the produce and consume methods require the calling thread to acquire the lock on the instance that the method is being called on.
发布的代码看起来做得很好,我唯一的挑剔是我会让方法抛出InterruptedException而不是捕获它。产生和使用方法都需要调用线程获取正在调用该方法的实例上的锁。