线程的优先级

public class PriorityTest extends JFrame{

    private static final long seriaVersionUID = 1L;
    private Thread threadA;
    private Thread threadB;
    private Thread threadC;
    private Thread threadD;
    
    public PriorityTest() {
        // TODO Auto-generated constructor stub
        final JProgressBar progressBar = new JProgressBar();
        final JProgressBar progressBar2 = new JProgressBar();
        final JProgressBar progressBar3 = new JProgressBar();
        final JProgressBar progressBar4 = new JProgressBar();
        getContentPane().add(progressBar);
        getContentPane().add(progressBar2);
        getContentPane().add(progressBar3);
        getContentPane().add(progressBar4);
        progressBar.setStringPainted(true);
        progressBar2.setStringPainted(true);
        progressBar3.setStringPainted(true);
        progressBar4.setStringPainted(true);
        threadA = new Thread(new MyThread(progressBar));
        threadB = new Thread(new MyThread(progressBar2));
        threadC = new Thread(new MyThread(progressBar3));
        threadD = new Thread(new MyThread(progressBar4));
        setPrioeity("threadA", 5, threadA);
        setPrioeity("threadB", 5, threadB);
        setPrioeity("threadC", 4, threadC);
        setPrioeity("threadD", 3, threadD);
    }
    
    public static void  setPrioeity(String threadName,int priority,Thread t) {
        t.setPriority(priority);
        t.setName(threadName);
        t.start();
    }
    public static void init(JFrame frame,int width,int height) {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.setSize(width, height);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        init(new PriorityTest(),100,100);
    }
    
    private static class MyThread implements Runnable{
        private final JProgressBar bar;
        int count = 0;
        
        public MyThread(JProgressBar Bar) {
            // TODO Auto-generated constructor stub
            this.bar= Bar;
        }
        
        
        public void run() {
            while (true) {
                bar.setValue(count+=10);
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println("当前线程被中断");
                }
            }
        }
    }

}

 

转载于:https://www.cnblogs.com/dulute/p/10650720.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值