Students who have used the bit download software should be aware that we have multiple download tasks executing at the same time, one or more of them is very important, so give these tasks a high priority so that the task can get more bandwidth to complete the download as soon as possible. Java threads have the same priority, and the higher the priority the scheduler gives it more CPU execution time, but note that if more than one thread is waiting for a lock, the higher the priority is, the sooner it can be executed.
Copy 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;
/**
* Priority of Threads
* 10 counter threads are set at different priority levels, we observe the effect of priority by adding the counter
* @author Godomi
* @blog http://blog.csdn.net/mq612
*/
public class Testmain extends JFrame {
Private Mythread [] thread = NULL; The thread to manipulate
Private JPanel pane = null;
Private JButton Startbutton = null, Stopbutton = NULL; Start, End button
Public Testmain () {
Super ("Thread priority");
pane = new JPanel ();
thread = new MYTHREAD[10];
for (int i = 0; i < i++) {//thread has a minimum priority of 1, 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 < i++) {
Thread[i].start ();
}
}
});
Stopbutton = new JButton ("End");
Stopbutton.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
for (int i = 0; i < 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 level
Private JLabel label = NULL; Priority Display Label
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); Add your own counters 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 to see the results
catch (Interruptedexception ex) {
Ex.printstacktrace ();
}
}
}
}
public static void Main (String [] args) {
New Testmain ();
}
}