Why this thread doesn't work?

前端 未结 1 1095
北恋
北恋 2020-12-21 11:33

I wrote this code to try threads on Android, but it doesn\'t work.

   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(save         


        
1条回答
  •  囚心锁ツ
    2020-12-21 12:19

    You're calling t.run() which means you're running all the code in the UI thread without starting a new thread.

    You should call t.start() which will instead start a new thread and execute the code in the run method within that new thread.

    (I'd also recommend implementing Runnable and then passing the Runnable to a new Thread constructor rather than overriding run, just as a matter of separation of concerns. It won't change the behaviour here, but it's a cleaner way of thinking about it IMO.)

    0 讨论(0)
提交回复
热议问题