JAVA线程的加入

断了今生、忘了曾经 提交于 2019-12-01 12:31:22
package lll;
import java.awt.*;
import javax.swing.*;
public class ccc extends JFrame{
    private Thread threadA;
    private Thread threadB;
    final JProgressBar pb1 =new JProgressBar();
    final JProgressBar pb2 =new JProgressBar();
    int count =0;
    public ccc(){
        super();
        getContentPane().add(pb1,BorderLayout.NORTH);
        getContentPane().add(pb2,BorderLayout.SOUTH);
        pb1.setStringPainted(true);
        pb2.setStringPainted(true);

    threadA =new Thread(new Runnable(){
        int count=0;
        public void run(){
            while(true){
                pb1.setValue(++count);
                try{
                    threadA.sleep(100);
                    threadB.join();
                }catch(Exception e){
                    e.printStackTrace();
                }
                if(count==100)
                    break;
            }
        }
    });
    threadA.start();
    threadB =new Thread(new Runnable(){
        int count =0;
        public void run() {
            while(true){
                pb2.setValue(++count);
                try{
                    Thread.sleep(100);
                }catch(Exception e){
                    e.printStackTrace();
                }
                if(count==100)
                    break;
            }
        }
        
    });
    threadB.start();
    }
    public static void main(String[] args){
        init(new ccc(),100,100);
    }
    private static void init(ccc ccc, int i, int j) {
        ccc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ccc.setSize(i,j);
        ccc.setVisible(true);
    }
}

    

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!