`

How to stop Thread

    博客分类:
  • j2se
阅读更多

Thread.stop()是一个不建议使用的方法。

 

 Thread in Java will stop once run() method finished. Another important point is that you can not restart a Thread which run() method has finished already , you will get an IllegalStateException.
所以正确让一个线程stop的方式是使用一个volatile变量来控制。

public class StoppableThread extends Thread {

   private volatile boolean stop = false;

   public void stopGracefully() {
     stop = true;
   }

   public void run() {
          while (!stop) {
       // long running action - finished will be true once work is done
     }
   }
}

 

 

分享到:
评论

相关推荐

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    Now that you’ve learned how to set breakpoints so the debugger will stop in your code, it’s time to get useful information out of whatever software you’re debugging. In this chapter you’ll learn ...

    VB编程资源大全(英文源码 API)

    1 , docudel.zip This example shows how to clear the document history on the start menu.<END><br>2 , BatteryAPIdemo.zip This example demonstrates how to "To get information about the battery ...

    Java邮件开发Fundamentals of the JavaMail API

    Instructions on how to download and install the JavaMail API are contained in the course. In addition, you will need a development environment such as the JDK 1.1.6+ or the Java 2 Platform, Standard...

    带优先级的多任务管理模块(C)

    1. End of the process, terminate the program only to stop and release all resources management, see 2) .2 contents of brackets 2. Call a function, this function does not write (taking into account ...

    微软内部资料-SQL性能优化2

    Before we look at how SQL Server uses and manages its memory, we need to ensure a full understanding of the more common memory related terms. The following definitions will help you understand how SQL...

    微软内部资料-SQL性能优化3

    For more information about how to decode this value, see also… Inside SQL Server 2000, pages 803 and 806. Key Range Locking Key Range Locking To support SERIALIZABLE transaction semantics, ...

    apktool documentation

    After you have the file locally, pay attention to how Apktool installs it. The number that the framework is named during install corresponds to the pkgId of the application. These values should range...

    A basic Windows service in VB (VBWindowsService)

    start and stop information to the Application event log, and shows how to run the main function of the service in a thread pool worker thread. You can easily extend the Windows Service skeleton to ...

    JProfiler Helper

    - Using triggers to start or stop profiling, take snapshots, or perform other actions. ##### Memory Profiling (A.2) **Recording Objects (A.2.1)** Recording objects is fundamental for identifying ...

    BobBuilder_app

    Multi-thread-able and parallel-able usage. Pages should be linked together so you can do range queries by going to the next page easily. The MGIndex MGIndex takes the best features of a b+tree and ...

    Sakemail

    Some stupid mail servers put tabs in some fields (CC:, TO:) when they want to make a new line, the correct is to put at least a space in the beginning of the line, added a little code to "...

    JLink_Windows_V648.zip

    DLL: Windows: Renesas RX: When using FINE interface and disabling ongoining debug mode on debug session close, it could happen that a thread was not exited gracefully, causing handle leaks. Fixed. DLL...

    SimIt-ARM-3.0 ARM指令模拟器

    SimIt-ARM-3.0-gk-20150902.tar.bz2 HowTo 0.what is SimIt-ARM-3.0 SimIt-ARM 3.0 is an instruction-set simulator that runs both system-level and user-level ARM programs, for more about it please read...

    手机游戏开发实例(smartphone联机炸弹人)

    Drawing Bitmaps to the screen........................................................................................................................ 14 II.1.4. Sprite movement and user input........

    Debugging with GDB --2001年5.3

    Contributors to GDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1 A Sample GDB Session . . . . . . . . . . . . . . . . . . . . 7 2 Getting In and Out of GDB . ....

    2021-2022计算机二级等级考试试题及答案No.18488.docx

    选项D的`Thread.stop()`方法已经过时,不建议使用,因为它可能引起不可预测的行为。 4. **空间复杂度**:算法的空间复杂度描述了执行算法时所需内存空间的大小,特别是关注算法执行过程中的最大需求。 5. **二维...

    Debugging with GDB --2007年

    5.4 Stopping and starting multi-thread programs . . . . . . . . . . . . . 6 Examining the Stack . . . . . . . . . . . . . . . . . . . . . . 51 6.1 6.2 6.3 6.4 6.5 6.6 7 Stack frames . . . . . . . . . ...

    最新版的DebuggingWithGDB7.05-2010

    4.12 Setting a Bookmark to Return to Later. . . . . . . . . . . . . . . . . . . . . . . 4.12.1 A Non-obvious Benefit of Using Checkpoints . . . . . . . . . . . . . 25 25 28 29 30 30 31 32 32 35 38 40 ...

    Debugging with GDB --2003年6.0

    5.4 Stopping and starting multi-thread programs . . . . . . . . . . . . . 6 Examining the Stack . . . . . . . . . . . . . . . . . . . . . . 53 6.1 6.2 6.3 6.4 7 Stack frames . . . . . . . . . . . . . ...

Global site tag (gtag.js) - Google Analytics