1.创建线程
public class MyThread extends Thread {
// 线程执行逻辑
@Override
public void run() {
// TODO: 线程执行的代码
}
}
2.实现线程方法
public class MyThread extends Thread {
@Override
public void run() {
// TODO: 线程执行的代码
System.out.println("线程执行逻辑");
}
}
3.创建线程对象
public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
}
}
4.启动线程
public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
5.插队
public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
try {
myThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
6.继续执行
public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
try {
myThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 继续执行的代码
System.out.println("继续执行");
}
}