簡體   English   中英

Android中的工作線程

[英]Worker thread in Android

您好我試圖模擬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 android.view.View.performClick(View.java:2538)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at android.view.View$PerformClick.run(View.java:9152)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at android.os.Handler.handleCallback(Handler.java:587)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at android.os.Looper.loop(Looper.java:130)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480):     at dalvik.system.NativeStart.main(Native Method)

所以這是你的解決方案。 StackOverflow已經有了答案。

為了在對象上調用wait(),notify()或notifyAll(),你必須首先擁有你想要調用方法的對象的監視器,所以在你運行的情況下,這就是你需要的方式去做吧:

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM