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()一种礼让方法,不过不保证当前线程会将系统资源礼让