2010-08-21 137 views
0

这是我正在使用的代码。无法停止线程

public class timerApp { 
Runnable timerRun = new runX(); 
Thread thread1 = new Thread(timerRun); 
public static void main(String[] args) { 
    timerApp xyz = new timerApp(); 
    xyz.createGUI(); 
} 
public void createGUI() { 
    JButton button = new JButton("Start timer"); 
    JButton button2 = new JButton("Stop timer"); 
    JFrame frame = new JFrame(); 
    JLabel label = new JLabel("under_construction"); 
    frame.getContentPane().add(BorderLayout.NORTH,button); 
    frame.getContentPane().add(BorderLayout.SOUTH,button2); 
    frame.getContentPane().add(BorderLayout.CENTER,label); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(500,500); 
    frame.setVisible(true); 
    button.addActionListener(new b1L()); 
    button2.addActionListener(new b2L()); 
} 
public class b1L implements ActionListener { 
    public void actionPerformed(ActionEvent event) { 
     thread1.start(); 
    } 
} 
public class b2L implements ActionListener { 
    public void actionPerformed(ActionEvent event) { 
     thread1.stop(); 
    } 
    } 
} 

我得到一个错误Note: timerApp.java uses or overrides a deprecated API. Note: Recompile with Xlint:deprecation for details. 我开始加入了停止按钮的监听器类后得到这个。

RELP!

回答

3

你得到的线程的stop()方法已被弃用此警告。

为什么使用Thread.stop已过时?

因为它本质上是不安全的。 停止线程会导致它解锁所有已锁定的监视器。 (作为 ThreadDeath异常向上传播 堆叠中的监视器解锁)。如果任何先前由这些监视器 保护的对象 的是处于不一致的状态,其他 线程现在可以 不一致的状态中查看这些对象。据称这些物体 被损坏。当线程 对损坏的对象进行操作时,可能导致任意的 行为。这种行为可能是微妙的并且难以检测到,或者 可能会发音。与其他 未经检查的异常不同,ThreadDeath 以静默方式杀死线程;因此,用户 没有警告他的程序可能损坏了 。在发生实际的 损坏后,腐败可以随时显示 本身,甚至在未来甚至可能是小时或天。

尼斯链接:

http://download.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

此链接提供的信息与您的问题:

  • 为什么使用Thread.stop已过时?
  • 我该用什么来代替 Thread.stop?
  • 如何停止长时间等待 的线程(例如,用于输入)?
  • 如果线程不响应 Thread.interrupt?

Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated?

5

首先,这不是一个错误。这是一个警告,您正在使用已弃用的方法。

Thread.stop()已被弃用。你可以阅读这个here背后的基本原理。

基本上他们的说法是,有终止来自外部的线程没有安全的方式;因此,你应该使用将很礼貌地询问线程的唯一办法阻止:

我应该用什么来代替 使用Thread.stop?

大部分停止使用应该 通过简单地 修改某些变量来表示 目标线程应该停止运行 代码来代替。目标线程应 定期检查该变量,并从一个 有序它的run方法 回报如果变量 表明它是停止运行。 (这是JavaSoft的 教程一直推荐的方式。)要 确保 停止请求的提示的通信,变量必须是 易失性(或接入到可变 必须同步)。

例如,假设你的applet 包含以下启动,停止和 运行方式:

private Thread blinker; 

public void start() { 
    blinker = new Thread(this); 
    blinker.start(); 
} 

public void stop() { 
    blinker.stop(); // UNSAFE! 
} 

public void run() { 
    Thread thisThread = Thread.currentThread(); 
    while (true) { 
     try { 
      thisThread.sleep(interval); 
     } catch (InterruptedException e){ 
     } 
     repaint(); 
    } 
} 

您可以通过替换applet的停止避免使用使用Thread.stop 和运行 方法有:

private volatile Thread blinker; 

public void stop() { 
    blinker = null; 
} 

public void run() { 
    Thread thisThread = Thread.currentThread(); 
    while (blinker == thisThread) { 
     try { 
      thisThread.sleep(interval); 
     } catch (InterruptedException e){ 
     } 
     repaint(); 
    } 
} 
6

stop()确实已经过时了。它会使程序处于不一致的状态。更好的方法是:

  1. Thread.interrupt()
  2. 设置一个布尔值,可见的线索,并获得该线程检查变量定期

关于如何安全地停止线程的详细信息,请参见here

+2

+1表示使用什么来代替'thread.stop()' – 2010-08-21 12:54:56