Java線程優先順序範例程式碼_java

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.