首页 > 代码库 > 通过运用多线程来异步控制程序的运行
通过运用多线程来异步控制程序的运行
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; } } |
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。