同一个Thread,只能start一次
//如下代码放入到点击事件里,第一次点击创建two这个Thread,之后的点击没有创建线程,出现错误
if (two == null) {
two = new Thread(new Test2Runnable(lockObject));
}
two.start();
错误如下:
2020-10-26 14:43:39.059 10224-10224/com.example.uxin.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.uxin.myapplication, PID: 10224
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:724)
at threadtest.ThreadTestActivity.onClick(ThreadTestActivity.java:83)
at android.view.View.performClick(View.java:6303)
at android.view.View$PerformClick.run(View.java:24828)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6798)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
网友评论