Java thread priority sample code

  • 2020-04-01 02:16:37
  • OfStack

For those of you who have used Bit to download software, it should be clear that we have multiple download tasks executing at the same time, and one or more of them is very important. Therefore, we set a high priority for these tasks, so that the tasks can get more bandwidth to complete the download as soon as possible. The priority of Java threads is similar, with the higher the priority the scheduler gives it more CPU execution time, but note that if multiple threads are waiting for a machine lock, the higher the priority, the earlier the execution.


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; 

/** 
 *  The priority of the thread  
 * 10 Each counter thread has a different priority, and we observe the effect of the priority by adding up the counter  
 * @author  bureau  
 * @blog http://blog.csdn.net/mq612 
 */
public class TestMain extends JFrame { 
    private MyThread [] thread = null; //The thread to operate on
    private JPanel pane = null; 
    private JButton startButton = null, stopButton = null; //Start and end buttons

    public TestMain(){ 
        super(" The priority of the thread "); 
        pane = new JPanel(); 
        thread = new MyThread[10]; 
        for(int i = 0; i < 10; i++){ //The minimum priority of a thread is 1 and the maximum is 10
            thread[i] = new MyThread(i + 1); 
        } 
        startButton = new JButton(" perform "); 
        startButton.addActionListener(new ActionListener(){ 
            public void actionPerformed(ActionEvent e) { 
                for(int i = 0; i < 10; i++){ 
                    thread[i].start(); 
                } 
            } 
        }); 
        stopButton = new JButton(" The end of the "); 
        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; //counter
        private int i = 0; //counter
        private int priority = 0; //priority
        private JLabel label = null; //Priority display labels
        private boolean b = true; //Boolean variable that controls the end of a thread

        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); //Adds its own counter to the main window pane
        } 
        
        public void quit(){ 
            b = false; 
        } 
        public void run(){ 
            while(b){ 
                this.text.setText(Integer.toString(i++)); 
                try { 
                    this.sleep(1); //Reducing the number of milliseconds here makes it easier for us to observe
                } catch (InterruptedException ex) { 
                    ex.printStackTrace(); 
                } 
            } 
        } 
    } 

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


Related articles: