线程实现有两种方法:

1.写一个类来继承Thread类,然后复写run()方法。

public class HelloThread extends Thread {
 
    public void run() {
        System.out.println("Hello from a thread!");
    }
 
    public static void main(String args[]) {
        (new HelloThread()).start();
    }
 
}

 

2.写一个类实现Runnable接口,然后复写run()方法。

public classHelloRunnable implements Runnable {
    public void run() {
        System.out.println("Hello from athread!");
    }
 
    public static void main(String args[]) {
        (new Thread(newHelloRunnable())).start();
    }
 
}

 

相关文章:

  • 2021-07-11
  • 2021-07-22
  • 2021-07-10
  • 2022-01-06
  • 2021-08-10
  • 2021-06-12
  • 2022-02-22
  • 2021-08-30
猜你喜欢
  • 2022-01-15
  • 2021-07-20
  • 2021-12-13
  • 2021-12-20
  • 2021-09-05
  • 2022-01-09
相关资源
相似解决方案