java 多线程

2021-05-25  本文已影响0人  媛猿YY

进程

线程

线程的创建

Thread类
Runnable接口
线程创建

通过继承Thread类的方式创建线程类,重写run()方法

线程的状态和生命周期

image.png
package com.example.javabase.xiancheng.join;

class Mythread extends Thread {
    public void run() {
        for (int i = 1; i <= 20; i++) {
            System.out.println(getName() + "正在执行" + i + "次!");
        }
    }

}

public class JoinDemo {
    public static void main(String[] args) {
        Mythread mt = new Mythread();
        mt.start();
        try {
            mt.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i <= 20; i++) {
            System.out.println("主线程运行第" + i + "次!");
        }
        System.out.println("主线程运行结束!");
    }
}

线程优先级

  1. 优先级常量
  1. 优先级相关方法


    image.png

多线程运行的问题

上一篇下一篇

猜你喜欢

热点阅读