一个关于线程的小问题.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Hello extends JFrame{
FlowLayout flowLayout=new FlowLayout();
JTextArea jTextArea1=new JTextArea(15,30);
JButton jButton1=new JButton("New Thread");
public Hello(){
try{
//this.setDefaultCloseOpration(EXIT_ON_CLOSE);
init();
this.setSize(350,400);
this.setVisible(true);
}catch(Exception ee){
ee.printStackTrace();
}
}
public static void main(String []args){
new Hello();
}
public void init(){
this.getContentPane().setLayout(flowLayout);
this.getContentPane().add(new JScrollPane(jTextArea1),null);
this.getContentPane().add(jButton1,null);
jButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ee){
new NewThread().run();
jTextArea1.append("Action finished\n");
}
});
}
class NewThread extends Thread{
public void run(){
try{
jTextArea1.append("Thread started\n");
Thread.sleep(5000);
jTextArea1.append("Thread finished\n");
}catch(Exception ee){
ee.printStackTrace();
}
}
}
}
我想知道,为什么Thread.sleep()会阻塞主线程.