有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android中的java工作线程

你好,我正在尝试在Android中模拟一个工作线程进行测试。 该活动有一个按钮,通过工作线程调用notify(),我希望它在bucle的10次迭代后进入睡眠状态

这是我的代码和错误的堆栈跟踪。我怎样才能解决它?谢谢

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    t= new Thread(){
        public void run(){
            int i=0;
            while(true){


                if (i%10==9){
                    //Simulated end of data until notified again
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            Log.d("Test",""+i);
            SystemClock.sleep(1000);
            } 
        }
    };
    t.start();


    findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            t.notify();

        }
    });
}





08-06 05:39:45.320: ERROR/AndroidRuntime(4480): FATAL EXCEPTION: main
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): java.lang.IllegalMonitorStateException: object not locked by thread before notify()
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at java.lang.Object.notify(Native Method)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at com.example.memorycrash.MemoryCrashTestActivity$2.onClick(MemoryCrashTestActivity.java:51)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at 安卓.view.View.performClick(View.java:2538)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at 安卓.view.View$PerformClick.run(View.java:9152)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at 安卓.os.Handler.handleCallback(Handler.java:587)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at 安卓.os.Handler.dispatchMessage(Handler.java:92)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at 安卓.os.Looper.loop(Looper.java:130)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at 安卓.app.ActivityThread.main(ActivityThread.java:3687)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at java.lang.reflect.Method.invoke(Method.java:507)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at dalvik.system.NativeStart.main(Native Method)

共 (1) 个答案

  1. # 1 楼答案

    这就是你的解决方案。 答案已经在StackOverflow上了

    要在对象上调用wait()、notify()或notifyAll(),必须首先拥有要调用该方法的对象的监视器,因此在runnable中,您需要这样做:

    Runnable runnable = new Runnable() {
        public void run() {
          // wait(); This call wouldn't work
          syncronized (this) {
            wait();  // This call will work
          }
        }
    };
    

    要通知runnable,还必须有监视器

    // runnable.notifyAll(); this call will not work
    syncronized (runnable) {
        runnable.notifyAll(); // this call will work
    }