JAVA线程178

package Multithreading;

public class MyThreadDemo {
    public static void main(String[] args) {
        MyThread my1 = new MyThread("飞机");
        MyThread my2 = new MyThread("火车");
        MyThread my3= new MyThread("自行车");
        my1.setPriority(Thread.MAX_PRIORITY);
        my2.setPriority(Thread.NORM_PRIORITY);
        my2.setPriority(Thread.MIN_PRIORITY);
        my1.start();
        my2.start();
        my3.start();
    }
}
package Multithreading;

public class MyThread extends Thread{
    public MyThread() {
    }

    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
       for (var i=0; i<100;i++){
           System.out.println(getName()+":"+i);
       }
    }
}

注意 因为JAVA的多线程是抢占式的调度模型 所以设置优先级并不能保证程序是按照优先级顺序执行 只是增加了线程获取的Cpu时间片的几率比较高,线程默认优先级是5 范围是1-10

posted @ 2022-04-05 21:58  phpwyl  阅读(16)  评论(0编辑  收藏  举报