栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java线程优先级示例代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


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();
    }  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/152979.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号