image.png

    1. package com.pln.thread;
    2. //创建线程方法:继承Thread类,重写run方法,调用start()方法开始线程
    3. //注意:线程开启不一定立即执行,由CPU决定
    4. public class TestThread1 extends Thread{
    5. @Override
    6. public void run() {
    7. //run方法体线程
    8. for (int i = 0; i < 20; i++) {
    9. System.out.println("我在打电脑");
    10. }
    11. }
    12. public static void main(String[] args) {
    13. // main线程,主线程
    14. // 创建一个线程对象
    15. TestThread1 testThread1 = new TestThread1();
    16. // 调用start方法开启线程
    17. testThread1.start();
    18. for (int i = 0; i < 1000; i++) {
    19. System.out.println("我在学习看书-------");
    20. }
    21. }
    22. }