background:

又一个持续运行的程序,不断产生数据,而在主程序中不仅需要监控所产生的程序,同时还要能控制其运行状态。

solution:

不断产生数据的为一个线程,为达到需求,增加两个线程,其中辅线程用来不断监视产生数据线程的数据,而主线程则用来控制辅线程的运行与否(即是否监视)。

一共涉及三个类,主控制类JController、辅线程Producer、数据线程Counter.代码如下所示:

1.JController

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
public class JController  extends JFrame{
	

	Counter count = new Counter();
	Producer produce = new Producer(count);
    public JController()
    {
        super();
        setTitle("JFileChooserTest");
        setBounds(100,100,350,150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        final JButton button = new JButton();
        JButton button2 = new JButton("button2");
        
        count.start();
        
        final JLabel label = new JLabel();
        button.addActionListener(new ActionListener(){  //监听事件
            public void actionPerformed(ActionEvent e){
            	boolean isAlive = produce.isAlive();
            	if(isAlive){
            		
            	}else{
            		produce = new Producer(count);
            		produce.start();
            	}
            
            }
        });
        
        button2.addActionListener(new ActionListener(){  //监听事件
            public void actionPerformed(ActionEvent e){
              produce.stopRunning();
            }
        });
        getContentPane().add(button,BorderLayout.NORTH);  //布局处理
        getContentPane().add(button2,BorderLayout.CENTER);
        button.setText("button1");
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JController jFileChooserTest = new JController();
        jFileChooserTest.setVisible(true);
    }

}

  2.Producer

public class Producer extends Thread{
	
	volatile boolean stopRequest = false;
	Counter count = new Counter();
	
	public Producer(Counter count){
		this.count = count;
	}
	
	@Override
	public void run(){
		while(!stopRequest){
			try {
				Thread.sleep(800);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				System.out.println(e.toString());
			}
			System.out.println(count.getCount());
		}
	}
	public void stopRunning(){
		stopRequest = true;
	}
	
	public static void main(String[] args){
	}
}

  3.Counter

import java.io.IOException;


public class Counter extends Thread{
	volatile int count =0;
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
	}
	public void run(){
		while(true){
		try {
			Thread.sleep(830);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			System.out.println(e.toString());
		}
		count++;
		}
	}
	public int getCount(){
		return count;
	}
}

  

posted on 2014-05-19 22:45  shanyang  阅读(201)  评论(0编辑  收藏  举报