点了开始按钮后,整个界面好像死掉了一样,其他的按钮均失效,请大家帮忙看下,代码如下:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
public class MainFrame2 extends JFrame implements ActionListener{
private static final long serialVersionUID = -2980848895240172944L;
public boolean isrunning;
Box box = Box.createHorizontalBox();
JButton jb1 = new JButton("开关");
JButton jb2 = new JButton("配置");
JButton jb3 = new JButton("使用说明");
//页签组件
JTabbedPane jtp1 = new JTabbedPane();
JPanel jp1 = new JPanel(); //实际中应该定义一个继承JPanel类来实现
JTabbedPane jtp2 = new JTabbedPane();
JPanel jp6 = new JPanel();
JTabbedPane jtp3 = new JTabbedPane();
JPanel jp7 = new JPanel();
//卡片布局
JButton start_btn = new JButton("启动");
JButton stop_btn = new JButton("停止");
CardLayout card = new CardLayout();
JPanel jps = new JPanel();
public MainFrame2()
{
//添加组件
init();
box.add(jb1);
box.add(jb2);
box.add(jb3);
//设置页签组件布局策略,一行显示
jtp1.add("控制开关",jp6);
jp6.add(start_btn);
jp6.add(stop_btn);
jtp2.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
jtp2.add("格式化",jp1);
jtp3.add("README",jp7);
jps.setLayout(card);
jps.add("control",jtp1);
jps.add("config",jtp2);
jps.add("readme",jtp3);
this.add(box,BorderLayout.NORTH);
this.add(jps,BorderLayout.CENTER);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
start_btn.addActionListener(this);
stop_btn.addActionListener(this);
//展现
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1)
{
card.show(jps, "control");
}
else if(e.getSource()==jb2)
{
card.show(jps, "config");
}else if(e.getSource()==jb3)
{
card.show(jps, "readme");
}else if(e.getSource()==start_btn)
{
setIsrunning(true);
startService();
}else if(e.getSource()==stop_btn)
{
setIsrunning(false);
stopService();
}
}
public static int POOLSIZE = 20; //线程池大小
public static int TIMEOUT = 10; //超时时间
public static int EXECUTECOUNT = 10; //线程池中保存的线程数
public static int TASKSLEEPTIME = 1000; //任务间隔时间
/**
* 创建线程池
*/
public static ThreadPoolExecutor threadPool;
private void createThreadPool(){
threadPool = new ThreadPoolExecutor(EXECUTECOUNT, POOLSIZE, TIMEOUT,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.CallerRunsPolicy());
}
public void stopService(){
// 第一阶段,不再接收新任务
threadPool.shutdown();
try {
// 等待若干时间给当前已经存在的任务,此处60秒
// 等待全部执行完毕就关闭,或者等待限制时间到来,关闭线程thread.interrupt
if (!threadPool.awaitTermination(60, TimeUnit.SECONDS)) {
// 取消当前存在的任务
threadPool.shutdownNow();
}
} catch (InterruptedException e) {
threadPool.shutdownNow();
// 保存中断状态
Thread.currentThread().interrupt();
}
}
public void startService() {
createThreadPool();
while(true) {
if(getIsrunning()==true){
threadPool.execute(new ReadMessageThread());
try {
Thread.sleep(TASKSLEEPTIME);
} catch (InterruptedException e) {
}
}
}
}
public void test(){
System.out.println("美女美女美女!");
}
class ReadMessageThread implements Runnable {
public synchronized void run(){
test();
}
}
public void init(){
}
public static void main(String[] args)
{
new MainFrame2();
}
public boolean getIsrunning() {
return isrunning;
}
public void setIsrunning(boolean isrunning) {
this.isrunning = isrunning;
}
}