`

Thread lifecycle

    博客分类:
  • JAVA
阅读更多

摘选自《JAVA in a Nutshell》

 

A thread can be in one of six states. In Java 5.0, these states are represented by the THRead.State enumerated type, and the state of a thread can be queried with the getState( ) method. A listing of the Thread.State constants provides a good overview of the lifecycle of a thread:


 

NEW

The Thread has been created but its start( ) method has not yet been called. All threads start in this state.


RUNNABLE

The thread is running or is available to run when the operating system schedules it.


BLOCKED

The thread is not running because it is waiting to acquire a lock so that it can enter a synchronized method or block. We'll see more about synchronized methods and blocks later in this section.


WAITING

The thread is not running because it has called Object.wait() or Thread.join( ) .


TIMED_WAITING

The thread is not running because it has called Thread.sleep() or has called Object.wait( ) or Thread.join() with a timeout value.


TERMINATED

The thread has completed execution. Its run( ) method has exited normally or by throwing an exception .

分享到:
评论

相关推荐

    SCJP 6 question demo

    - **Thread Lifecycle:** When a thread terminates due to an unhandled exception, it simply stops executing. Any code after the point of the exception will not run. In the provided code, the main thread...

    Learning.NET.High.Performance.Programming

    Dive deep into the internals of CLR, from memory management to the thread lifecycle A step-by-step guide with a special focus on designing performance-oriented solutions to handle large datasets Who ...

    SCJP6 Sun Certificated Programmer for Java 6 Study Guide (Exam 310-065) 英文原版

    - **Thread Lifecycle**: States of a thread: new, runnable, blocked, dead. - **Thread Creation**: Extending Thread class or implementing Runnable interface. - **Synchronization**: Preventing concurrent...

    Android代码-SugarTask

    SugarTask Android lifecycle safety task with sugar code style. Support Android API 14 and UP. If you have any question or ...At your MainThread(UIThread), start a background thread just like this: Suga

    rxlifecycle-android-2.2.2.zip

    .observeOn(AndroidSchedulers.mainThread()) .subscribe(aLong -> { // 这里进行UI更新操作 }); ``` 在这个例子中,当Activity销毁时,`bindToLifecycle()`会自动取消订阅,防止了内存泄露。 关于...

    Android代码-Thunder

    Thunder Android OkHttp util package let response callback at MainThread(UIThread), also it‘s lifecycle safety. ⚠️ Thunder‘s code is based on SugarTask(Very nice code

    Java多线程初学者指南:线程的生命周期.pdf

    例如,在示例代码中创建了`LifeCycle`类的一个实例`thread1`,此时线程还没有开始执行,`isAlive()`方法返回`false`。 2. **就绪(Runnable)**: 当调用`start()`方法时,线程从新建状态转变为就绪状态,准备由...

    9. 多线程2.rar

    可以使用LifeCycleManager类来监控应用程序的状态,并在适当的时候停止或恢复线程。 至于压缩包中的"09-4.swf"和"09-5.swf"文件,它们可能包含与多线程相关的动画教程或者演示,帮助视觉化理解多线程的运行机制。...

    Enumerating Running Processes in Win95 and WinNT (107KB)

    4. **TEB (Thread Environment Block)**:每个线程也有一个TEB结构,与PEB类似,但存储的是线程相关的数据。 5. **CreateToolhelp32Snapshot/Process32First/Process32Next**:这些函数组合在一起,可以用来遍历...

    eclipse_eventbus3.0jar包.zip

    6. **订阅者生命周期**:订阅者可以标记为`@Subscriber(lifeCycle = Lifecycle.Event.ON_START)`或`@Subscriber(lifeCycle = Lifecycle.Event.ON_RESUME)`,以在特定的生命周期事件(如Activity的onStart或onResume...

    Android 生命周期架构组件使用方法

    首先,需要在 build.gradle 文件中添加依赖项:`implementation 'androidx.lifecycle:lifecycle-runtime:2.3.0'`。然后,在 Activity 或 Fragment 中创建一个 LifecycleDelegate 对象,用于管理生命周期。例如: ``...

    DotNet面试题

    1. Describe the difference between a Thread and a Process? 2. What is a Windows Service and how does its lifecycle differ from a "standard" EXE? 3. What is the maximum amount of memory any ...

    Java_for_the_Web_with_Servlets

    - **Servlet Lifecycle**: The lifecycle of a servlet includes initialization (`init()`), service (`service()`), and destruction (`destroy()`). - **Configuration Information**: Servlets can obtain ...

    AutoDispose,自动绑定+处理RXJava 2流。.zip

    4. **兼容性**:AutoDispose不仅支持Android组件,还支持使用Lifecycle库的组件,如AndroidX的Lifecycle-aware components。 5. **扩展性**:它允许开发者自定义绑定策略,以适应特定的场景和需求。 6. **与其他库...

    Android高级进阶学习下载

    4. **Android Jetpack**:这是Google推出的一系列库,包括Lifecycle、Room、ViewModel、Navigation等组件,旨在简化开发流程,提高代码的可读性和可维护性。 5. **多线程与异步处理**:Android中的AsyncTask、...

    7个代表性的Android应用程序完整源代码

    10. **生命周期管理**:源码会展示如何正确管理Activity和Fragment的生命周期,以及如何使用Lifecycle库进行更高效的资源管理。 11. **权限管理**:Android 6.0以后,部分敏感权限需要在运行时动态申请。源码中可能...

    以数字线索理念为指导的系统生命周期管理技术研究.docx

    数字线索(Digital Thread)的理念是这一领域的核心,旨在通过全面的建模、数据分析和仿真评估来量化和减少系统生命周期中的不确定性,支持装备生命周期的关键决策,减少复杂系统开发的时间和成本。 数字线索的特点...

    AndroidBackgroundThreadCoroutine:Hasil Belajar Android背景线程Dengan Menggunakan Kotlin协程

    lifecycle.coroutineScope.launch { // 启动协程 } } override fun onDestroy() { super.onDestroy() // 取消协程 lifecycle.coroutineScope.cancel() } ``` 使用Kotlin协程处理Android背景线程,不仅可以...

Global site tag (gtag.js) - Google Analytics