1. 创建一个Thread子类
    2. 重写run方法
    3. 将要做的事情协助run方法体内
    4. 通过对象调用start方法
    5. 一个对象只能调用一个start方法
    6. 可以创建多个对象调用多个start方法
    7. 存在线程安全问题

    示例代码:

    1. /**
    2. * 创建人:LYY
    3. * 创建时间:2022/4/25
    4. *
    5. * 多线程创建方式一:
    6. * 1. 创建一个继承Thread类的子类
    7. * 2. 重写Thread类的run方法
    8. * 3. 将要执行的内容写在run方法体内
    9. * 4. 通过对象调用当前子类的start方法
    10. */
    11. public class ThreadOne extends Thread{
    12. @Override
    13. public void run() {
    14. // 输出1-100的偶数
    15. for (int i = 0; i < 100; i++) {
    16. if (i % 2 == 0) {
    17. System.out.println("i = " + i);
    18. }
    19. }
    20. }
    21. }
    1. /**
    2. 创建人:LYY
    3. 创建时间:2022/4/25
    4. */
    5. public class ThreadTest {
    6. public static void main(String[] args) {
    7. ThreadOne threadOne = new ThreadOne();
    8. // 调用线程 线程一
    9. // start方法 1. 启动线程 2. 使java虚拟机调用run方法
    10. threadOne.start();
    11. // 直接调用run方法无法启动多线程功能
    12. threadOne.run();
    13. // 一个对象只能调用一个start方法 否则会出现IllegalThreadStateException
    14. //threadOne.start();
    15. // 新建对象 在调用start方法
    16. ThreadOne threadOne1 = new ThreadOne();
    17. // 新的线程启动 线程二
    18. threadOne1.start();
    19. for (int i = 0; i < 100; i++) {
    20. if (i % 2 != 0) {
    21. System.out.println("i = " + i);
    22. }
    23. }
    24. }
    25. }