image.png

    1. package com.pln.thread;
    2. //创建线程方式2:实现Runnable接口,重写run方法,执行线程需要传入一个Runnable接口实现类,调用start方法;
    3. public class TestThread3 implements Runnable {
    4. @Override
    5. public void run() {
    6. // run方法线程体
    7. for (int i = 0; i <20 ; i++) {
    8. System.out.println("我在学习");
    9. }
    10. }
    11. public static void main(String[] args) {
    12. //创建Runnable接口实现类对象
    13. TestThread3 testThread3 = new TestThread3();
    14. //创建线程对象,通过线程对象来开启线程(把testThread3对象传给Thread类)
    15. Thread thread = new Thread(testThread3);
    16. thread.start();
    17. for (int i = 0; i <800 ; i++) {
    18. System.out.println("你好加油---------");
    19. }
    20. }
    21. }

    image.png