Java多线程系列(3)

中断Thread的方法:(by Johnny Deng)

1)标记法:通过线程不断判断标记的条件是否满足来决定线程是否退出。这种方法可能会出现中断的延迟。即当前线程如果处于bloking状态,可能无法读取条件。这种方法的例子如下:

包含两个类,一个线程类,一个控制类。

/*-**************************************************************/
 *
 *      Package Name: seibuaa.concurrency.series3
 *      File    Name; StopbyFlag.java
 *      Description :
 *      Author      : DengXiong
 *      Copyright   : SEI BUAA
 *      Date        : 2006-10-20
 *
/*-**************************************************************/
package seibuaa.concurrency.series3;

public class StopbyFlag extends Thread {
 private volatile boolean finished = false;
 
 public void run(){
  while (!finished){
   try {
    sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   doSomething();
  }
 }
 
 public void doSomething(){
  
  System.out.println("Do something!");
 }
 
 public void setFinished(boolean flag){
  finished = flag;
 }

}

/*-**************************************************************/
 *
 *      Package Name: seibuaa.concurrency.series3
 *      File    Name; SwingTester.java
 *      Description :
 *      Author      : DengXiong
 *      Copyright   : SEI BUAA
 *      Date        : 2006-10-20
 *
/*-**************************************************************/
package seibuaa.concurrency.series3;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SwingTester extends JFrame {

 /**
  * @param args
  */
 public static void main(String[] args) {
  SwingTester tester = new SwingTester();
  tester.show();
 }
 
 public SwingTester(){

  this.initComponents();
 }
 
 
 private void initComponents(){
  
  JPanel p = new JPanel();
  setBounds(100, 100, 400, 400);
  add(p, BorderLayout.SOUTH);
  
  stopButton = new JButton();
  stopButton.setText("Stop");
  startButton = new JButton();
  startButton.setText("Start");
  p.add(startButton);
  p.add(stopButton);
        addWindowListener( new WindowAdapter() {
            public void windowClosed( WindowEvent e ) {
             if (worker != null){
              worker.setFinished(true);
             }
                System.exit(0);
            }
        });
        setDefaultCloseOperation( DISPOSE_ON_CLOSE );
  stopButton.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent e) {
    startButton.setEnabled(true);
    stopButton.setEnabled(false);
    worker.setFinished(true); 
    try {
     Thread.sleep(1000);
    } catch (InterruptedException e1) {
     e1.printStackTrace();
    }
    worker = null;
   }
   
  });
  startButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    startButton.setEnabled(false);
    stopButton.setEnabled(true);    
    if (worker == null){
     worker = new StopbyFlag();
    }
    worker.setFinished(false); 
    worker.start();
   }
   
  });
  
 }
 
 private JButton stopButton;
 private JButton startButton;
 private StopbyFlag worker;

}

2)中断线程的方法:即调用interrupt()方法。这种方法会立即终断线程。不论它是否存在bloking的状态(如果存在,立即被唤醒)。

例子如下:

两个类,同上。

/*-**************************************************************/
 *
 *      Package Name: seibuaa.concurrency.series3
 *      File    Name; StopByInterruption.java
 *      Description :
 *      Author      : DengXiong
 *      Copyright   : SEI BUAA
 *      Date        : 2006-10-20
 *
/*-**************************************************************/
package seibuaa.concurrency.series3;

public class StopByInterruption extends Thread{
 private volatile boolean finished = false;
 
 public void run(){
  while (!isInterrupted()){
   try {
    sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   doSomething();
  }
 }
 
 public void doSomething(){
  
  System.out.println("Do something!");
 }
 
 

}

/*-**************************************************************/
 *
 *      Package Name: seibuaa.concurrency.series3
 *      File    Name; SwingTesterbyInterruption.java
 *      Description :
 *      Author      : DengXiong
 *      Copyright   : SEI BUAA
 *      Date        : 2006-10-20
 *
/*-**************************************************************/
package seibuaa.concurrency.series3;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SwingTesterbyInterruption extends JFrame{


 /**
  * @param args
  */
 public static void main(String[] args) {
  SwingTester tester = new SwingTester();
  tester.show();
 }
 
 public SwingTesterbyInterruption(){

  this.initComponents();
 }
 
 
 private void initComponents(){
  
  JPanel p = new JPanel();
  setBounds(100, 100, 400, 400);
  add(p, BorderLayout.SOUTH);
  
  stopButton = new JButton();
  stopButton.setText("Stop");
  startButton = new JButton();
  startButton.setText("Start");
  p.add(startButton);
  p.add(stopButton);
        addWindowListener( new WindowAdapter() {
            public void windowClosed( WindowEvent e ) {
             
                System.exit(0);
            }
        });
        setDefaultCloseOperation( DISPOSE_ON_CLOSE );
  stopButton.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent e) {
    startButton.setEnabled(true);
    stopButton.setEnabled(false);
    worker.interrupt(); 

    worker = null;
   }
   
  });
  startButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    startButton.setEnabled(false);
    stopButton.setEnabled(true);    
    if (worker == null){
     worker = new StopByInterruption();
    }
    worker.start();
   }
   
  });
  
 }
 
 private JButton stopButton;
 private JButton startButton;
 private StopByInterruption worker;

}

 

 

你可能感兴趣的:(java,thread,多线程,Date,null,import)