下面的三个例子使用了不同的方式完成了同样的事情。

实现Runnnable的方式:
void startAThread0() {
  new Thread(new MyRunnable()).start();
}
 
class MyRunnable implements Runnable {
  public void run() {
    ...
  }
}


继承Thread的方式:
void startAThread1() { new MyThread().start(); } class MyThread extends Thread { public void run() { ... } }


匿名继承Thread的方式:
void startAThread2() { new Thread() { public void run() { ... } }.start(); }

不要直接调用run()方法。总是调用Thread.start()方法,这个方法会创建一条新的线程并使新建的线程调用run()。

 

相关文章:

  • 2022-01-16
  • 2021-06-05
  • 2022-12-23
  • 2021-12-03
  • 2021-06-29
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案