Android 中的工作线程

发布于 2024-11-27 23:49:08 字数 2611 浏览 0 评论 0原文

你好,我正在尝试在 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)

Hello im trying to simulate a worker thread in Android for testing.
The activity has a button that calls notify() over the worker thread, and I want it go to sleep after 10 iterations of the bucle.

Thats my code and the stacktrace of the error. How can I solve it? Thanks

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攀登最高峰 2024-12-04 23:49:08

所以这是您的解决方案。
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.notifyAll(); this call will not work
syncronized (runnable) {
    runnable.notifyAll(); // this call will work
}

So here is your solution.
Answer was already there on StackOverflow.

In order to call wait(), notify(), or notifyAll() on an object you must first own the monitor of the object you wish to call the method on, so in you case within the runnable this would be how you would need to do it:

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

To notify that runnable you would also have to have the monitor

// runnable.notifyAll(); this call will not work
syncronized (runnable) {
    runnable.notifyAll(); // this call will work
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文