中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java線程優(yōu)先級(jí)示例

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用

使用過Bit下載軟件的同學(xué)應(yīng)該很清楚,我們有多個(gè)下載任務(wù)同時(shí)執(zhí)行,而其中的某一個(gè)或多個(gè)是非常重要的,于是給這些任務(wù)設(shè)定一個(gè)高度優(yōu)先,以便任 務(wù)可以獲取更多的帶寬盡早完成下載。Java線程的優(yōu)先級(jí)也差不多,優(yōu)先級(jí)越高排程器就會(huì)給它越多的CPU執(zhí)行時(shí)間,但請(qǐng)注意:如果有多個(gè)線程在等待一個(gè) 機(jī)鎖的時(shí)候,并不是優(yōu)先級(jí)越高就可以越早執(zhí)行。

TestMain.java

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;

/**
 * 線程的優(yōu)先級(jí)
 * 10個(gè)計(jì)數(shù)器線程分別被設(shè)置了不同的優(yōu)先級(jí),我們通過計(jì)數(shù)器的累加來觀察優(yōu)先級(jí)的作用
 * @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; // 啟動(dòng)、結(jié)束按鈕

    public TestMain(){
        super("線程的優(yōu)先級(jí)");
        pane = new JPanel();
        thread = new MyThread[10];
        for(int i = 0; i < 10; i++){ // 線程的優(yōu)先級(jí)最小是1,最大是10
            thread[i] = new MyThread(i + 1);
        }
        startButton = new JButton("執(zhí)行");
        startButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                for(int i = 0; i < 10; i++){
                    thread[i].start();
                }
            }
        });
        stopButton = new JButton("結(jié)束");
        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);
    }
    /**
     * 計(jì)數(shù)器線程
     */
    class MyThread extends Thread{
        private JTextField text = null; // 計(jì)數(shù)器
        private int i = 0; // 計(jì)數(shù)器
        private int priority = 0; // 優(yōu)先級(jí)
        private JLabel label = null; // 優(yōu)先級(jí)顯示標(biāo)簽
        private boolean b = true; // 控制線程結(jié)束的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); // 將自己的計(jì)數(shù)器加入主窗口面板中
        }
        /**
         * 結(jié)束線程
         */
        public void quit(){
            b = false;
        }
        public void run(){
            while(b){
                this.text.setText(Integer.toString(i++));
                try {
                    this.sleep(1); // 減小這里的毫秒數(shù),可以讓我們更容易觀察到結(jié)果
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    public static void main(String [] args){
        new TestMain();
    }

}

標(biāo)簽: seo

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:base64編碼加密解密的JavaScript實(shí)現(xiàn)

下一篇:JS簡(jiǎn)單的拖動(dòng)類