Three ways to create multiple threads in Java
1, inheritance Thread class to create a thread
2, to achieve Runnable interface to create threads
3, using the Callable and Future create a thread
-------------------------------------------------- ----------------------------------------------Dividing line-- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------
First, create a thread to inherit the Thread class
/** * @ Author: ZhuZhu * @ Date :Created in 16:54 2019/6/9 * @ Description: */ public class MyThread extends Thread { // 重写run方法 public void run(){ for (int i = 1;i <= 5;i++){ System.out.println("Thread :"+i); } } }
public class Test1 {
public static void main(String[] args) throws InterruptedException {
MyThread thread = new MyThread();
thread.start();
}
}
Print Results:
-------------------------------------------------- ----------------------------------------------Dividing line-- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------