Students who have used Bit download software should be very clear that we have multiple download tasks executed at the same time, and one or more of them are very important, so we set a high priority for these tasks so that the tasks can obtain more Bandwidth completes the download as early as possible. The priorities of Java threads are similar. The higher the priority, the more CPU execution time the scheduler will give it. However, please note: if there are multiple threads waiting for a machine lock, it does not mean that the higher the priority, the more CPU execution time it will have. The sooner it can be executed.
Copy the code code as follows:
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;
/**
* Thread priority
* The 10 counter threads are set with different priorities. We observe the effect of priorities through the accumulation of counters.
* @author five buckets of rice
* @blog http://blog.csdn.net/mq612
*/
public class TestMain extends JFrame {
private MyThread [] thread = null; // Thread to operate on
private JPanel pane = null;
private JButton startButton = null, stopButton = null; // Start and end buttons
public TestMain(){
super("Thread priority");
pane = new JPanel();
thread = new MyThread[10];
for(int i = 0; i < 10; i++){ // The minimum thread priority is 1 and the maximum is 10
thread[i] = new MyThread(i + 1);
}
startButton = new JButton("Execute");
startButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < 10; i++){
thread[i].start();
}
}
});
stopButton = new JButton("End");
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);
}
/**
* Counter thread
*/
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 label
private boolean b = true; // boolean variable that controls the end of the 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); // Add your own counter to the main window panel
}
/**
* End thread
*/
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 can make it easier for us to observe the results
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
public static void main(String [] args){
new TestMain();
}
}