国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java線程優先級示例代碼

2019-11-26 15:58:55
字體:
來源:轉載
供稿:網友

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蕲春县| 平凉市| 怀集县| 兴仁县| 清水河县| 江阴市| 灵璧县| 博兴县| 集贤县| 德兴市| 靖西县| 萨迦县| 白城市| 甘泉县| 田东县| 乌拉特中旗| 上饶县| 宕昌县| 雷山县| 长乐市| 大理市| 临海市| 满城县| 项城市| 海晏县| 白城市| 庆云县| 成都市| 乌苏市| 二连浩特市| 伊金霍洛旗| 台山市| 石棉县| 景德镇市| 静乐县| 舟曲县| 华池县| 同德县| 南皮县| 谷城县| 西吉县|