from http://download.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
Why Are
|
Why is Thread.stop
deprecated?
Because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked. (The monitors are unlocked as the ThreadDeath
exception propagates up the stack.) If any of the objects previously protected by these monitors were in an inconsistent state, other threads may now view these objects in an inconsistent state. Such objects are said to be damaged. When threads operate on damaged objects, arbitrary behavior can result. This behavior may be subtle and difficult to detect, or it may be pronounced. Unlike other unchecked exceptions, ThreadDeath
kills threads silently; thus, the user has no warning that his program may be corrupted. The corruption can manifest itself at any time after the actual damage occurs, even hours or days in the future.
Couldn't I just catch the ThreadDeath
exception and fix the damaged object?
In theory, perhaps, but it would vastly complicate the task of writing correct multithreaded code. The task would be nearly insurmountable for two reasons:
- A thread can throw a
ThreadDeath
exception almost anywhere. All synchronized methods and blocks would have to be studied in great detail, with this in mind. - A thread can throw a second
ThreadDeath
exception while cleaning up from the first (in thecatch
orfinally
clause). Cleanup would have to repeated till it succeeded. The code to ensure this would be quite complex.
What about Thread.stop(Throwable)
?
In addition to all of the problems noted above, this method may be used to generate exceptions that its target thread is unprepared to handle (including checked exceptions that the thread could not possibly throw, were it not for this method). For example, the following method is behaviorally identical to Java's throw
operation, but circumvents the compiler's attempts to guarantee that the calling method has declared all of the checked exceptions that it may throw:
static void sneakyThrow(Throwable t) { Thread.currentThread().stop(t); }
What should I use instead of Thread.stop
?
Most uses of stop
should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. (This is the approach that the Java Tutorial has always recommended.) To ensure prompt communication of the stop-request, the variable must be volatile (or access to the variable must be synchronized).
For example, suppose your applet contains the following start
, stop
and run
methods:
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(); } }You can avoid the use of
Thread.stop
by replacing the applet's stop
and run
methods with:
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(); } }
How do I stop a thread that waits for long periods (e.g., for input)?
That's what the Thread.interrupt
method is for. The same "state based" signaling mechanism shown above can be used, but the state change (blinker = null
, in the previous example) can be followed by a call to Thread.interrupt
, to interrupt the wait:
public void stop() { Thread moribund = waiter; waiter = null; moribund.interrupt(); }For this technique to work, it's critical that any method that catches an interrupt exception and is not prepared to deal with it immediately reasserts the exception. We sayreasserts rather than rethrows, because it is not always possible to rethrow the exception. If the method that catches the
InterruptedException
is not declared to throw this (checked) exception, then it should "reinterrupt itself" with the following incantation:
Thread.currentThread().interrupt();This ensures that the Thread will reraise the
InterruptedException
as soon as it is able.
What if a thread doesn't respond to Thread.interrupt
?
In some cases, you can use application specific tricks. For example, if a thread is waiting on a known socket, you can close the socket to cause the thread to return immediately. Unfortunately, there really isn't any technique that works in general. It should be noted that in all situations where a waiting thread doesn't respond to Thread.interrupt
, it wouldn't respond to Thread.stop
either. Such cases include deliberate denial-of-service attacks, and I/O operations for which thread.stop and thread.interrupt do not work properly.
Why are Thread.suspend
and Thread.resume
deprecated?
Thread.suspend
is inherently deadlock-prone. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume
, deadlock results. Such deadlocks typically manifest themselves as "frozen" processes.
What should I use instead of Thread.suspend
and Thread.resume
?
As with Thread.stop
, the prudent approach is to have the "target thread" poll a variable indicating the desired state of the thread (active or suspended). When the desired state is suspended, the thread waits using Object.wait
. When the thread is resumed, the target thread is notified using Object.notify
.
For example, suppose your applet contains the following mousePressed event handler, which toggles the state of a thread called blinker
:
private boolean threadSuspended; Public void mousePressed(MouseEvent e) { e.consume(); if (threadSuspended) blinker.resume(); else blinker.suspend(); // DEADLOCK-PRONE! threadSuspended = !threadSuspended; }You can avoid the use of
Thread.suspend
and Thread.resume
by replacing the event handler above with:
public synchronized void mousePressed(MouseEvent e) { e.consume(); threadSuspended = !threadSuspended; if (!threadSuspended) notify(); }and adding the following code to the "run loop":
synchronized(this) { while (threadSuspended) wait(); }The
wait
method throws the InterruptedException
, so it must be inside a try ... catch
clause. It's fine to put it in the same clause as the sleep
. The check should follow (rather than precede) the sleep
so the window is immediately repainted when the the thread is "resumed." The resulting run
method follows:
public void run() { while (true) { try { Thread.currentThread().sleep(interval); synchronized(this) { while (threadSuspended) wait(); } } catch (InterruptedException e){ } repaint(); } }Note that the
notify
in the mousePressed
method and the wait
in the run
method are inside synchronized
blocks. This is required by the language, and ensures that wait
and notify
are properly serialized. In practical terms, this eliminates race conditions that could cause the "suspended" thread to miss a notify
and remain suspended indefinitely.
While the cost of synchronization in Java is decreasing as the platform matures, it will never be free. A simple trick can be used to remove the synchronization that we've added to each iteration of the "run loop." The synchronized block that was added is replaced by a slightly more complex piece of code that enters a synchronized block only if the thread has actually been suspended:
if (threadSuspended) { synchronized(this) { while (threadSuspended) wait(); } }
In the absence of explicit synchronization, threadSuspended must be made volatile to ensure prompt communication of the suspend-request.
The resultingrun
method is:
private boolean volatile threadSuspended; public void run() { while (true) { try { Thread.currentThread().sleep(interval); if (threadSuspended) { synchronized(this) { while (threadSuspended) wait(); } } } catch (InterruptedException e){ } repaint(); } }
Can I combine the two techniques to produce a thread that may be safely "stopped" or "suspended"?
Yes; it's reasonably straightforward. The one subtlety is that the target thread may already be suspended at the time that another thread tries to stop it. If the stop method merely sets the state variable (blinker) to null, the target thread will remain suspended (waiting on the monitor), rather than exiting gracefully as it should. If the applet is restarted, multiple threads could end up waiting on the monitor at the same time, resulting in erratic behavior.To rectify this situation, the stop method must ensure that the target thread resumes immediately if it is suspended. Once the target thread resumes, it must recognize immediately that it has been stopped, and exit gracefully. Here's how the resulting run and stop methods look:
public void run() { Thread thisThread = Thread.currentThread(); while (blinker == thisThread) { try { thisThread.sleep(interval); synchronized(this) { while (threadSuspended && blinker==thisThread) wait(); } } catch (InterruptedException e){ } repaint(); } } public synchronized void stop() { blinker = null; notify(); }If the stop method calls Thread.interrupt, as described above, it needn't call notify as well, but it still must be synchronized. This ensures that the target thread won't miss an interrupt due to a race condition.
What about Thread.destroy
?
Thread.destroy
has never been implemented. If it were implemented, it would be deadlock-prone in the manner of Thread.suspend
. (In fact, it is roughly equivalent to Thread.suspend
without the possibility of a subsequent Thread.resume
.) We are not implementing it at this time, but neither are we deprecating it (forestalling its implementation in future). While it would certainly be deadlock prone, it has been argued that there may be circumstances where a program is willing to risk a deadlock rather than exit outright.
Why is Runtime.runFinalizersOnExit
deprecated?
Because it is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock. While this problem could be prevented if the class whose objects are being finalized were coded to "defend against" this call, most programmers do notdefend against it. They assume that an object is dead at the time that its finalizer is called.
Further, the call is not "thread-safe" in the sense that it sets a VM-global flag. This forces every class with a finalizer to defend against the finalization of live objects!
Copyright © 1995-99 Oracle and/or its affiliates. All rights reserved. Suggest a feature or make comments |
|
发表评论
文章已被作者锁定,不允许评论。
-
将 JBoss封装为Windows 服务
2012-03-16 16:21 494Download javaService form objec ... -
java ee specifications
2012-03-15 16:29 666java ee specifications -
JEE - Memory Status Jsp Using JMX
2012-02-03 10:42 891<!-- jsp file content --& ... -
转载:负载均衡会话保持技术、原理、产品(以F5为例)
2011-12-29 14:26 645___________________ ... -
转载:Session详解[郎云鹏]
2011-12-28 10:08 5转载文章,原文发表于 @ 2004年12月08日 bea ... -
temporay
2011-12-08 17:30 820http://community.jboss.org/wiki ... -
Monitoring and Management Using JMX
2011-11-11 17:19 770http://download.oracle.com/j ... -
[REF] Java EE Version History
2011-07-12 14:01 687simply copied from : http://en ... -
保持原始存入顺序的Map实现类
2011-07-11 16:20 884LinkedHashMap
相关推荐
自.NET 2.0以来,Thread.Suspend()与Thread.Resume()方法已过时,VS提示可以使用如Monitor等技术,但是对于刚接触同步控制的人来说理解起来太复杂。本人利用Thread.Abort()与Thread.Interrupt()可以引起目标线程异常...
1. **任务管理**:RTT的任务(Task)是系统执行的基本单元,开发者可以通过`rt_thread_create`创建任务,`rt_thread_startup`启动任务,`rt_thread_delete`删除任务,以及`rt_thread_suspend`和`rt_thread_resume`...
### 一、Suspend 和 Resume 概念 在嵌入式系统及移动设备(如Android设备)中,Suspend 和 Resume 是两个非常重要的概念。当设备进入 Suspend 状态时,它会暂时停止所有非必要的功能以节省电力,而在 Resume 状态下...
休眠/唤醒在嵌入式Linux中是非常重要的部分,嵌入式设备尽可能的进入休眠状 态来延长电池的续航时间.这篇文章就详细介绍一下Linux中休眠/唤醒是如何工作 的, 还有Android中如何把这部分和Linux的机制联系起来的.
在C#中,线程控制方法主要包括Thread.Start()、Thread.Abort()、Thread.Suspend()和Thread.Resume()等。 1. Thread.Start():启动线程的执行,线程从开始执行到结束。 2. Thread.Abort():终止线程的执行,线程立即...
然而,`Thread.suspend()`和相应的`Thread.resume()`方法已不推荐使用,因为它们可能导致死锁。现在更推荐使用同步机制(如`synchronized`关键字)或`wait()`/`notify()`方法来实现类似功能。 线程的唤醒则是指将...
* tx_thread_resume():恢复线程的执行 * tx_thread_sleep():使线程进入睡眠状态 * tx_thread_suspend():暂停线程的执行 * tx_thread_terminate():终止线程的执行 * tx_thread_wait_abort():等待线程的终止 ...
// thread.resume(); // 恢复线程(与suspend配合使用) // thread.stop(); // 停止线程(不推荐,因为不安全) // thread.interrupt(); // 中断线程,通常用于通知线程退出循环 } } ``` 在用户级线程调度中,...
Android PM(电源管理)的suspend-resume机制是优化电池寿命的重要组成部分,它涉及到低功耗模式、动态电压和频率调整以及时钟与电源开关控制。 1. 低功耗模式:嵌入式芯片通常具有多种低功耗状态,如STOP、WAIT和...
这些操作需要使用到`Thread`类的方法,如`Thread.Suspend()`(暂停)、`Thread.Resume()`(恢复)、`Thread.Interrupt()`(中断)和`Thread.Priority`(设置优先级)。 实现这个功能时,你需要监听文本框的事件,如...
- 不推荐使用Thread.stop()、Thread.suspend()和Thread.resume(),因为它们可能导致不可预测的行为。 8. **线程池**: - **ExecutorService**:Java提供的线程池服务,可以管理线程的生命周期,提高系统效率。 -...
传统的做法如使用`Thread.Suspend`、`Thread.Resume`和`Thread.Abort`在.NET框架中存在潜在风险。`Suspend`和`Resume`可能会导致死锁,而`Abort`可能导致资源泄露和未捕获的异常。因此,更安全的做法是通过设置共享...
开发者可以使用`Thread.Suspend()`和`Thread.Resume()`来暂停和恢复线程,但这些方法在.NET Framework 4.0及以后版本已被标记为不推荐使用,因为它们可能导致死锁。更安全的方式是使用`ThreadPool`或异步编程模型如`...
ThreadGroup 还提供了一些其他的方法,例如 stop() 方法可以停止当前 ThreadGroup 中的所有线程,resume() 方法可以恢复当前 ThreadGroup 中的所有线程,suspend() 方法可以暂停当前 ThreadGroup 中的所有线程等。...
在 Sun 公司的一篇文章《Why are Thread.stop, Thread.suspend and Thread.resume Deprecated? 》中详细讲解了舍弃这些方法的原因。这篇文章解释了 stop() 方法可能会导致线程处于不确定状态,suspend() 方法可能会...
- `runtime.Suspend()`, `runtime.Resume()`: 尽管Go标准库没有提供这些函数,但理解如何通过通道或互斥锁控制goroutine的暂停和恢复也是很重要的。 3. **CPU和系统资源管理** - `runtime.GOMAXPROCS(n int) int`...
ThreadStart start = new ThreadStart(Work); Thread thread = new Thread(start); ... thread.Suspend(); Thread.Sleep(3000); Console.WriteLine("挂起后继续执行线程"); thread.Resume();
在本文中,我们将深入探讨NXP i.MX RT1052处理器上的RT-Thread实时操作系统(RTOS)的线程管理。NXP i.MX RT1052是一款高性能、低延迟的跨界微控制器,特别适合工业应用和物联网(IoT)设备。RT-Thread是一个轻量级、...
1.1.1 CPU and Memory ................................................................................................. 21 1.1.2 Clocks and Power Management ...............................................