Java线程之线程优先级

线程优先级

  

使用过Bit下载软件的同学应该很清楚,我们有多个下载任务同时执行,而其中的某一个或多个是非常重要的,于是给这些任务设定一个高度优先,以便任务可以获取更多的带宽尽早完成下载。
Java线程的优先级也差不多,优先级越高排程器就会给它越多的CPU执行时间,但请注意:如果有多个线程在等待一个机锁的时候,并不是优先级越高就可以越早执行。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
/**
 * 线程的优先级
 * 10个计数器线程分别被设置了不同的优先级,我们通过计数器的累加来观察优先级的作用
 * @author 五斗米
 * @blog http://blog.csdn.net/mq612
 */
public class TestMain extends JFrame {
    private MyThread [] thread = null; // 要操作的线程
    private JPanel pane = null;
    private JButton startButton = null, stopButton = null; // 启动、结束按钮
   
    public TestMain(){
        super("线程的优先级 ");
        pane = new JPanel();
        thread = new MyThread[10];
        for(int i = 0; i < 10; i++){ // 线程的优先级最小是 1,最大是10
            thread[i] = new MyThread(i + 1);
        }
        startButton = new JButton("执行 ");
        startButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                for(int i = 0; i < 10; i++){
                    thread[i].start();
                }
            }
        });
        stopButton = new JButton("结束 ");
        stopButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                for(int i = 0; i < 10; i++){
                    thread[i].quit();
                }
            }
        });
        JPanel p = new JPanel();
        p.add(startButton);
        p.add(stopButton);
        this.getContentPane().add(pane);
        this.getContentPane().add(p, BorderLayout.NORTH);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500, 300);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
    /**
     * 计数器线程
     */
    class MyThread extends Thread{
        private JTextField text = null; // 计数器
        private int i = 0; // 计数器
        private int priority = 0; // 优先级
        private JLabel label = null; // 优先级显示标签
        private boolean b = true; // 控制线程结束的 boolean变量
       
        public MyThread(int priority){
            this.priority = priority;
            this.setPriority(priority);
            JPanel p = new JPanel();
            label = new JLabel("Priority=" + priority);
            text = new JTextField(12);
            p.add(label);
            p.add(text);
            pane.add(p); // 将自己的计数器加入主窗口面板中
        }
        /**
         * 结束线程
         */
        public void quit(){
            b = false;
        }
        public void run(){
            while(b){
                this.text.setText(Integer.toString(i++));
                try {
                    this.sleep(1); // 减小这里的毫秒数,可以让我们更容易观察到结果
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
   
    public static void main(String [] args){
        new TestMain();
    }
   
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值