Java中的线程实现

线程实现有两种方法:

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();
    }
 
}
原文地址:https://www.cnblogs.com/tianzhijiexian/p/3985368.html