下面是关于是线程的优先权的设置
import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JProgressBar; public class PriorityTest extends JFrame { /** * */ private static final long serialVersionUID = -7759161002167557345L; private Thread threadA; private Thread threadB; private Thread threadC; private Thread threadD; final JProgressBar progressBarA=new JProgressBar(); final JProgressBar progressBarB=new JProgressBar(); final JProgressBar progressBarC=new JProgressBar(); final JProgressBar progressBarD=new JProgressBar(); @SuppressWarnings("unused") private int count=0; public PriorityTest() { super(); getContentPane().setLayout(new GridLayout(4,1,20,20)); getContentPane().add(progressBarA); getContentPane().add(progressBarB); getContentPane().add(progressBarC); getContentPane().add(progressBarD); progressBarA.setStringPainted(true); progressBarB.setStringPainted(true); progressBarC.setStringPainted(true); progressBarD.setStringPainted(true); threadA=new Thread(new MyThread(progressBarA)); threadB=new Thread(new MyThread(progressBarB)); threadC=new Thread(new MyThread(progressBarC)); threadD=new Thread(new MyThread(progressBarD)); setPriority("threadA",10,threadA); setPriority("threadB",5,threadB); setPriority("threadC",4,threadC); setPriority("threadD",1,threadD); } public static void setPriority(String threadName,int priority,Thread t) { t.setPriority(priority); //设置线程的进程优先权,最大值为10,最小值为1 t.setName(threadName); //main中优先权为5 t.start(); } public static void main(String[] args) { init(new PriorityTest(),100,100); } public static void init(JFrame frame,int width,int height) { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500,600); frame.setVisible(true); } private final class MyThread implements Runnable { private final JProgressBar bar; int count=0; private MyThread(JProgressBar bar) { this.bar=bar; } public void run() { while(true) { bar.setValue(count+=1); try { Thread.sleep(1000); }catch(Exception e) { System.out.println("当前线程被中断"); } } } } }