码迷,mamicode.com
首页 > 编程语言 > 详细

通过运用多线程来异步控制程序的运行

时间:2014-05-20 09:21:53      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:blog   class   c   code   java   tar   

background:

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

solution:

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

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

1.JController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
    }
}

  

通过运用多线程来异步控制程序的运行,布布扣,bubuko.com

通过运用多线程来异步控制程序的运行

标签:blog   class   c   code   java   tar   

原文地址:http://www.cnblogs.com/shanyangmanbu/p/3737882.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!