线程的优先级

package thread;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JProgressBar;

/*
 * 线程的优先级
 * 每个线程都具有各自的优先级,线程的优先级可以表明在程序中该线程的重要性,如果有很多线程处于就绪状态,
 * 系统会根据优先级来决定哪个线程进入运行状态。但并不意味着低优先级的线程得不到运行,而只是它运行的几率
 * 比较小,如垃圾回收线程的优先级就较低。
 * 线程的优先级可以使用setPriority()方法调整,如果使用该方法设置的优先级不在1~10之内,
 * 将产生IllegalArgumentException异常
 */
public class PriorityTest extends JFrame{
    /*
     * 每个线程都有一个"优先级",优先级可以用整数表示,取值范围为0~10,0为最低优先级,10位最高优先级,
     * 当决定哪个线程需要调度时,首先查看是否存在优先级高的可调度线程,如果存在,就从中选择进行调度。
     * 当决定哪个线程需要调度时,首先查看是否存在优先级高的可调度线程,如果存在,就从中选择进行调度。
     * 当该线程的时间片到达之后,系统查看是否存在另一个优先级比较高的可调度线程,如果存在就调度。
     * 这样依次进行判断调度线程。本实例用两种方式来实现线程优先级排列家族等级-1静态常量表示;2用数字表示。
     * 实现线程优先级
     * (1)Thread类有三个优先级静态常量:MAX_PRIORITY为10,为线程最高优先级;
     * MIN_PRIORITY取值为1,为线程最低优先级;NORM_PRIORITY取值为5,为线程中间位置的优先级。
     * 默认情况下,线程的优先级为NORM_PRIORITY。
     * (2)Java中的线程在同等情况下,对于两个同时启动的线程,优先级高的线程先获取CPU资源,先被真正运行
     * ,优先级低的线程后获取CPU资源,后被执行。特殊情况在于现在计算机都是多核多线程的配置,
     * 有可能优先级低的线程比优先级高的线程先运行,优先级高的线程可能比优先级低的线程后运行,
     * 线程的执行先后还是由Java虚拟机调度决定的。
     */
    
    public PriorityTest() {
        super();
        //创建进度条组件
        final JProgressBar pb1=new JProgressBar();
        final JProgressBar pb2=new JProgressBar();
        final JProgressBar pb3=new JProgressBar();
        final JProgressBar pb4=new JProgressBar();
        //设置进度条显示数字字符
        pb1.setStringPainted(true);
        pb2.setStringPainted(true);
        pb3.setStringPainted(true);
        pb4.setStringPainted(true);
        //将进度条放置在窗体合适位置。
        getContentPane().add(pb1,BorderLayout.NORTH);
        getContentPane().add(pb2,BorderLayout.SOUTH);
        getContentPane().add(pb3,BorderLayout.WEST);
        getContentPane().add(pb4,BorderLayout.EAST);
        //分别实例化四个线程
        Thread threadA=new Thread(new MyThread(pb1));
        Thread threadB=new Thread(new MyThread(pb2));
        Thread threadC=new Thread(new MyThread(pb3));
        Thread threadD=new Thread(new MyThread(pb4));
        setPriority("threadA", 5, threadA);
        setPriority("threadB", 5, threadB);
        setPriority("threadC", 4, threadC);
        setPriority("threadD", 3, threadD);
    }
    //定义设置线程的名称,优先级的方法
    public static void setPriority(String threadName,int priority,Thread t) {
        //设置线程优先级
        t.setPriority(priority);
        //设置线程名称
        t.setName(threadName);
        //启动线程
        t.start();
    }
    public static void init(JFrame frame,int width,int height) {
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        init(new PriorityTest(),100,100);
    }
    //定义一个实现Runnable接口的类
    private final class MyThread implements Runnable{
        private final JProgressBar bar;
        int count =0;
        private MyThread(JProgressBar bar) {
            this.bar=bar;
        }
        @Override
        public void run() {
            while(true) {
                bar.setValue(count+=10);//设置滚动条的值每次自增10
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                System.out.println("当前线程被中断");
                }
            }
            
        }
    }

    
}

猜你喜欢

转载自blog.csdn.net/qq_41978199/article/details/80651364