线程的优先权

来源:互联网 发布:applem2引擎全套源码 编辑:程序博客网 时间:2024/04/27 21:36
import java.awt.BorderLayout;import java.awt.Container;import javax.swing.JFrame;import javax.swing.JProgressBar;public class Jointest extends JFrame{/** *  */private static final long serialVersionUID = 1L;private Thread threadA;private Thread threadB;final JProgressBar progressBar=new JProgressBar();final JProgressBar progressBar2=new JProgressBar();int count=0;public static void main(String[] args){init(new Jointest(),100,100);}public Jointest(){super();Container c=getContentPane();//添加两个进度条c.add(progressBar,BorderLayout.NORTH);c.add(progressBar2,BorderLayout.SOUTH);//设置进度条显示数字progressBar.setStringPainted(true);progressBar2.setStringPainted(true);    threadA=new Thread(new Runnable(){int count=0;public void run(){while(true){progressBar.setValue(++count);try{Thread.sleep(100);threadB.join();             //线程的加入}catch(Exception e){e.getStackTrace();}}}});threadA.start();threadB=new Thread(new Runnable(){int count=0;public void run(){while(true){progressBar2.setValue(++count);try{Thread.sleep(100);}catch(Exception e1){e1.printStackTrace();}if(count==100)break;}}});threadB.start();}public static void init(JFrame frame,int width,int heigth){frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(width,heigth);frame.setVisible(true);}}

下面是关于是线程的优先权的设置

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,最小值为1t.setName(threadName);               //main中优先权为5t.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("当前线程被中断");}}}}}


0 0
原创粉丝点击