Java中启动线程的三种方式及其各自优势有哪些?
- 内容介绍
- 文章标签
- 相关推荐
本文共计711个文字,预计阅读时间需要3分钟。
1. 继承Thread类,并调用start方法启动线程。javaclass MyThread extends Thread { // 重写run方法,线程运行的内容 public void run() { // System.out.println(); }}
public static void main(String[] args) { Thread t1=new MyThread(); t1.start();}
1. 继承 Thread 类,然后调用 start 方法。
class MyThread extends Thread { //重写run方法,线程运行后,跑的就是run方法 public void run(){ //System.out.println(""); } public static void main(String[] args){ Thread t1 = new MyThread(); t1.start(); //线程运行,调用的 run()方法. } }
2. 实现 Runnable 接口的 run 方法, 然后再用 Thread 类包裹后,调用 start 方法。
本文共计711个文字,预计阅读时间需要3分钟。
1. 继承Thread类,并调用start方法启动线程。javaclass MyThread extends Thread { // 重写run方法,线程运行的内容 public void run() { // System.out.println(); }}
public static void main(String[] args) { Thread t1=new MyThread(); t1.start();}
1. 继承 Thread 类,然后调用 start 方法。
class MyThread extends Thread { //重写run方法,线程运行后,跑的就是run方法 public void run(){ //System.out.println(""); } public static void main(String[] args){ Thread t1 = new MyThread(); t1.start(); //线程运行,调用的 run()方法. } }
2. 实现 Runnable 接口的 run 方法, 然后再用 Thread 类包裹后,调用 start 方法。

