void | join(long millis) 等待该线程终止的时间最长为 millis 毫秒。 |
---|
void | join() 等待该线程终止。 |
---|
下面的例子是两个进度条,向进度条A中加入进度条B,让A等待2000ms
package defaul;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class JoinText extends JFrame{
private static final long serialVersionUID = 1L;
private Thread threadA;
private Thread threadB;
final JProgressBar progressBar = new JProgressBar();
final JProgressBar progressBar2 = new JProgressBar();
public static void main(String[] args) {
new JoinText().setVisible(true);
}
public JoinText(){
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;
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
progressBar.setValue(++count);
try {
threadA.sleep(100);
if(count==20){
threadB.join(2000);//线程B加入线程A至多等待200ms
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
threadA.start();
threadB = new Thread(new Runnable() {
int count = 0;
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
progressBar2.setValue(++count);
try {
threadA.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
threadB.start();
setBounds(200,200,300,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}