由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - java synchronized 问题
相关主题
condional variable thread sync 问题 (转载)科普贴,fusion IO
求教一个Java问题 IllegalMonitorStateException哈,居然写完了作业
A try-catch problem in C++java里用synchronized包住block就可以保护多线程同步问题了,这就是c里面的mutex吧?
C++ Q17: throw 2Volatile variables do not provide any atomicity (转载)
C++ Q20: construction and inheritance给大家出个多进程的题
请教register一个JAVA程序请教
Question on synchronization between processesAnybody help me on these questions?
C++ Q15: throwHow does X Input Method work?
相关话题的讨论汇总
话题: putint话题: notifyall话题: getint话题: index
进入Programming版参与讨论
1 (共1页)
j****g
发帖数: 597
1
今天看programer_interview看到一个producer-customer并行的问题,用java写的(第7
章section 7.12). Program is written as:
class producer extends Thread {
......
void run() {
while (true) {
try {
putInt();
}
catch (...){};
}
}
private synchronized void putInt() throws ... {
while (index == MAX_CAPACITY) {
wait()
}
buffer[index] = ...
index++;
notifyAll();
}
private synchronized int getInt() throws ... {
notifyAll(); // I ha
s****u
发帖数: 118
2
getint wait了

第7

【在 j****g 的大作中提到】
: 今天看programer_interview看到一个producer-customer并行的问题,用java写的(第7
: 章section 7.12). Program is written as:
: class producer extends Thread {
: ......
: void run() {
: while (true) {
: try {
: putInt();
: }
: catch (...){};

j****g
发帖数: 597
3
getInt的wait好像应该由putInt的notifyAll来释放吧?
m******t
发帖数: 2416
4

Not necessarily. All the producers and consumers are waiting on the same
object. So a notifyAll from either method would wake up all of them.

【在 j****g 的大作中提到】
: getInt的wait好像应该由putInt的notifyAll来释放吧?
j****g
发帖数: 597
5
so my question is,
when the buffer is full, and getInt is invoked, it will notifyAll, releasing
the putInt that was blocked on the wait.
If putInt kicks in before getInt could modify the index, then there will be
race condition.
Is that correct?
m******t
发帖数: 2416
6

releasing
be
There won't be.
The putInt thread, after waking up, would have to get hold of the lock
before it can actually resume execution. The getInt thread does _not_
relinguish its ownership on the lock just by calling notifyAll().

【在 j****g 的大作中提到】
: so my question is,
: when the buffer is full, and getInt is invoked, it will notifyAll, releasing
: the putInt that was blocked on the wait.
: If putInt kicks in before getInt could modify the index, then there will be
: race condition.
: Is that correct?

j****g
发帖数: 597
7
So does that mean "synchronized" keyword is actually putting lock on the
producer ocject itself but not the method. You can only enter EITHER putInt
OR getInt but you can't enter both from different threads.
right?
g*****g
发帖数: 34805
8
The lock and the monitor is always on object.

putInt

【在 j****g 的大作中提到】
: So does that mean "synchronized" keyword is actually putting lock on the
: producer ocject itself but not the method. You can only enter EITHER putInt
: OR getInt but you can't enter both from different threads.
: right?

1 (共1页)
进入Programming版参与讨论
相关主题
How does X Input Method work?C++ Q20: construction and inheritance
ask a C question about random number请教register
请教一个MS Linked List的问题Question on synchronization between processes
问几个问题C++ Q15: throw
condional variable thread sync 问题 (转载)科普贴,fusion IO
求教一个Java问题 IllegalMonitorStateException哈,居然写完了作业
A try-catch problem in C++java里用synchronized包住block就可以保护多线程同步问题了,这就是c里面的mutex吧?
C++ Q17: throw 2Volatile variables do not provide any atomicity (转载)
相关话题的讨论汇总
话题: putint话题: notifyall话题: getint话题: index