两个进度条

package button2;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class getmain extends JFrame{
 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private Thread threadA;//创建2个Thread对象
 private Thread threadB;
 final JProgressBar bar1=new JProgressBar();//定义两个进度条
 final JProgressBar bar2=new JProgressBar();
 int count=0;
 public static void main(String []args) {
  init(new getmain(),100,100);
 }
 public getmain() {
  super();
  getContentPane().add(bar1,BorderLayout.NORTH);//边界布局
  getContentPane().add(bar2,BorderLayout.SOUTH);
  bar1.setStringPainted(true);
  bar2.setStringPainted(true);
  threadA=new Thread(new Runnable() {//实现Runnable接口
   int count=0;
   public void run() {                    //重写run()
    while(true) {
     bar1.setValue(++count);       //设置进度条数字字符
     try {
      Thread.sleep(100);//a休眠100Ms
      threadB.join();//B调用join()方法-暂停当前进程,直到B进程运行完
      
     }catch(Exception e) {
      e.printStackTrace();
     }
    }
   }
  });
  threadA.start();
  threadB=new Thread(new Runnable() {
   int count=0;
   public void run() {
    while(true) {
     bar2.setValue(++count);
     try {
      Thread.sleep(100);
      
     }catch(Exception e) {
      e.printStackTrace();
     }
     if(count==100)
      break;
    }
   }
  });
  threadB.start();
 }
 public static void init(JFrame frame,int width,int height) {
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(width, height);
  frame.setVisible(true);
 }
}


///线程中断

1,一般采用BOOLEAN变量在run()方法中

2.interupted()中断,会抛出异常

3.join()加入另外一个线程使当前线程中断,直到加入线程执行完毕

4.yield()一种礼让方法,不过不保证当前线程会将系统资源礼让

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值