ansguocheng 2010-03-16 14:40 采纳率: 100%
浏览 226
已采纳

关于AWT死循环监听

点了开始按钮后,整个界面好像死掉了一样,其他的按钮均失效,请大家帮忙看下,代码如下:

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;
}

}

  • 写回答

1条回答 默认 最新

  • xxxxxxxxxxxxxxxxx 2010-03-16 15:15
    关注

    //帮你简单修改了一下:

    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(final ActionEvent e) {
    
          Thread worker = new Thread() {
              public void run() {
                  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();
                }
    
              }
            };
    
            worker.start(); // So we don't hold up the dispatch thread.
    
    
    
    
    
    
    }
    
    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.currentThread().sleep(TASKSLEEPTIME);
                } catch (InterruptedException e) {
                }
            }
        }
    }
    
    public void test() {
    
        System.out.println("美女美女美女!");
        Thread.yield();
    }
    
    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;
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试