一个关于线程的小问题.

lefteye 2004-03-03 11:45:08
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()会阻塞主线程.
...全文
47 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧