package com.suse.thread;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class JoinTest extends JFrame {
private Thread threadA;
private Thread threadB;
final JProgressBar progressBar = new JProgressBar();
final JProgressBar progressBar2 = new JProgressBar();
int count = 0;
public JoinTest() {
super("进程控制实现进度条");
getContentPane().add(progressBar, BorderLayout.NORTH);
getContentPane().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(1000);
threadB.join();// 使用线程B调用join()方法
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
threadA.start();
threadB = new Thread(new Runnable() {
int count = 0;
public void run() {
while (true) {
progressBar2.setValue(++count);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
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);
}
public static void main(String[] args) {
init(new JoinTest(), 100, 100);
}
}
线程控制之进度条
最新推荐文章于 2024-01-05 09:00:00 发布