由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - a fun coding question
相关主题
问一个 java generic问题出个简单题,看你Java APi熟悉到什么程度
看到一个关于singleton的面试题Ask a simple question about throw exception, bow bow bow
how to read registry key value using java (64-bit system)Simp question on thread synchronization
问个primitive type的问题如何让两个socket并行执行thread
问个autoboxing的问题multi-threading guru们 (转载)
问个简单的Java技术问题CC150 16.6答案是不是有问题? (转载)
Alternative way to swap Integer关于java的疑惑 (转载)
Integer in heapjava synchronized 问题 (转载)
相关话题的讨论汇总
话题: public话题: exception话题: integer话题: void话题: synobject
进入Java版参与讨论
1 (共1页)
c******n
发帖数: 4965
1
what's wrong with following code?
just try it ...
public class block {
Integer a = new Integer(0);
void dead() throws Exception {
synchronized ( a ) {
while ( a == 0 )
a.wait();
}
}
public void release() {
synchronized(a) {
a = 1;
a.notifyAll();
}
}
public void demo() throws Exception {
new Thread() {
public void run() { try { dead() ; } catch (Exception e )
{System.out.pri
f****m
发帖数: 38
2
不就是release()这个method拿不到a的monitor么?

【在 c******n 的大作中提到】
: what's wrong with following code?
: just try it ...
: public class block {
: Integer a = new Integer(0);
: void dead() throws Exception {
: synchronized ( a ) {
: while ( a == 0 )
: a.wait();
: }
: }

q*********u
发帖数: 280
3
Integers are immutable. If you want to affect the value of a Integer
variable, the only way is to create a new Integer object and discard the old
one.
在你这个
a = 1
a.notifyAll();
这里新的Integer的对象出来了,已经不是原来那个a所指的对象,这个时候在notify,
就会出现illegalMonitorStateException
这个题的确是比较阴险的。

【在 c******n 的大作中提到】
: what's wrong with following code?
: just try it ...
: public class block {
: Integer a = new Integer(0);
: void dead() throws Exception {
: synchronized ( a ) {
: while ( a == 0 )
: a.wait();
: }
: }

c******n
发帖数: 4965
4
the problem is, normal usage of java notify() never mention that the
mutex/lock needs to be the same as the monitor.
q*********u
发帖数: 280
5
package multithread;
class SynObject{
public int counter;
public SynObject(){
counter = 0;
}
public SynObject(int cnt){
counter = cnt;
}
}
class Thread3 extends Thread{
public SynObject obj;

public Thread3(SynObject obj){
this.obj = obj;
}

public void run(){
synchronized(obj){
System.out.println("Thread3, before while() lo

【在 c******n 的大作中提到】
: the problem is, normal usage of java notify() never mention that the
: mutex/lock needs to be the same as the monitor.

1 (共1页)
进入Java版参与讨论
相关主题
java synchronized 问题 (转载)问个autoboxing的问题
问一个lock的puzzle问个简单的Java技术问题
关于Vector的土问题Alternative way to swap Integer
how to run java program in dos?Integer in heap
问一个 java generic问题出个简单题,看你Java APi熟悉到什么程度
看到一个关于singleton的面试题Ask a simple question about throw exception, bow bow bow
how to read registry key value using java (64-bit system)Simp question on thread synchronization
问个primitive type的问题如何让两个socket并行执行thread
相关话题的讨论汇总
话题: public话题: exception话题: integer话题: void话题: synobject