Java IllegalMonitorStateException:对象在notify()之前未被线程锁定
是的,我以前见过类似主题的问题,但它们似乎对我没有帮助。如果有重复的,请告诉我这个问题,谢谢 我这里有两个问题,第一个是标题问题:Java IllegalMonitorStateException:对象在notify()之前未被线程锁定,java,android,multithreading,Java,Android,Multithreading,是的,我以前见过类似主题的问题,但它们似乎对我没有帮助。如果有重复的,请告诉我这个问题,谢谢 我这里有两个问题,第一个是标题问题: @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //Starts up method1 & method2 in a try-catch try{ method1();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Starts up method1 & method2 in a try-catch
try{
method1();
method2();
new CountDownLatch(1);
CountDownLatch.await();
}catch(InterruptedException e){
// TODO Auto-generated catch
// block
e.printStackTrace();
}
//This Toast should only run after notify() is called
Toast.makeText(firstClass.this, "This is toasty", Toast.LENGTH_SHORT).show();
}
//Start of method 1, which is currently useless
public void method1() throws InterruptedException{
//Creates new thread
Thread thread = new Thread(new Runnable(){
@Override
public void run(){
try{
synchronized(SecondClass.this){
}
}catch(Exception e){
e.printStackTrace();
}
}
});
thread.start();
}
public void method2(){
Thread thread2 = new Thread(new Runnable(){
@Override
public void run(){
//Sets a button, when the button is clicked, then the code is continued
final Button bNext = (Button) findViewById(R.id.bIsoAbunSave);
bNext.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0){
synchronized(SecondClass.this){
CountDownLatch.countDown();
}
}
});
}
});
thread2.start();
}
据我所知,如果wait()为false,请纠正我,wait()的作用是暂停所有正在运行的代码,直到某个事件(如单击按钮)导致调用notify()
第二个问题是,在我点击按钮之前,祝酒词就出现了。我假设,当我调用wait()时,所有代码都会等到我单击按钮,然后调用notify()并继续代码。但实际情况是,Toast似乎忽略了wait()并以任何方式运行 如果有任何问题,请直接提问,谢谢 编辑:logcat消息显示崩溃发生在:
synchronized(SecondClass.this){notify();}
这里
您锁定了findViewById
和setOnClickListener
的调用,但没有锁定onClick方法,因此调用时notify
在任何同步块之外
您应该在onClick
方法中移动锁,该方法是代码块到块的方法
//Sets a button, when the button is clicked, then the code is continued
final Button bNext = (Button) findViewById(R.id.bIsoAbunSave);
bNext.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0){
synchronized(TopClass.this){ notify(); }
}
});
(请记住,此
在内部类中是不同的)
无论如何,不建议使用<代码>等待/通知< /C> >,考虑使用JDK中的其他多线程类和集合中的一个。
在这里,您可以同步
Runnable
而不是主类。我已经更改了代码,但仍然会发生错误。我刚刚将“this”改为“SecondClass.this”(这是代码所在的类),但错误消息仍然存在。无论如何,这是个例外,因为当thread2启动时,thread1没有启动yetSo是否会有线程。sleep(500)修复了吗?编辑:Woops,这没有意义,因为您没有使用单线程程序。可能发生的情况是,即使在500毫秒之后,Android仍然没有启动线程,因为它现在无法启动或类似的事情。无论如何,要解决这个问题,还是Android就是这样?“第二个问题是,甚至在我单击按钮之前,就会出现吐司。我在调用wait()时假设,所有代码都将等待,直到我单击该按钮,其中将调用notify(),代码将继续。但实际情况是,Toast似乎忽略了wait(),并以任何方式运行。“这是因为代码中有3个线程,一个UI线程继续执行,以及您创建的2个不同线程,UI线程从线程创建中跳转并继续,因此它将面对您的吐司!!!“wait()的作用是停止所有正在运行的代码”不确定所有正在运行的代码是什么意思。foo.wait()。它不会“停止”任何其他线程。
//Sets a button, when the button is clicked, then the code is continued
final Button bNext = (Button) findViewById(R.id.bIsoAbunSave);
bNext.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0){
synchronized(TopClass.this){ notify(); }
}
});
synchronized(this){
//These statements *should* make the code wait until I click the button
wait();
}