# Java学习疑点(4)--线程的六个状态以及其安全性问题的个例解析

Threads can be in one of six states:

•  New
•  Runnable
•  Blocked
•  Waiting
•  Timed waiting
•  Terminated

Each of these states is explained in the sections that follow.

To determine the current states of a threads, simply call the getState method.

When you create a thread with the new operator--the thread is not yet running.This means that it is in thenewstate.When a thread is in the new state, the programe has not started executing code inside of it.A certain amount of bookkeeping needs to be done before a thread can run.

Once you invoke the start method, the thread is in the runnable state.A runnable thread may or may not actually be running.It is up to the operating system to give the thread time to run.(The Java specification doen not call this a separate state, though. A running thread is still in the runnable state.)

Once a thread is running,  it doesn't necessarily keep running.In fact, it is desirable if running threads occasionally pause so that other threads have a chance to run. The details of thread scheduling systems give each runnable thread a slice of time to perform its task.When that slice of time is  exhausted, the operating system preempts the thread and gives another thread an opportunity to work.

All modern desktop and server operating systems use preemptive scheduling. However, small devices such as cell phones may use cooperating scheduling. In such a device, a thread loses control only when it calls the yield method,  or it is blocked or waiting.

On a machine with multiple processors, each  processor can run a thread, and you can hava multiple threads run in parallel. Of course, if there are more threads than processors, the scheduler still has to do time-slicing.

Always keep in mind that a runnable thread may or may ont be running at any given time.(This is why the states is called "runnable" and not "running.")

When a thread is blocked ot waiting, it is temporarily inactive. It doesn't execute any code and it consumes mininal resources. It is up to the thread scheduler to reactivate it.

The details depend on  how the inactive state was reached.

• When the thread tries to acquire an intrinsic object lock(buy not a Lock in the java.util.concurrent library) that is currently held by another thread, it becomes blocked. The thread becomes unblocked when all other threads have relinquished the lock and the thread scheduler has allowed this thread to hold it.
• When the thread waits for another thread to notify the scheduler of a condition, it enters the waiting states. This happens by calling the object.wait or Thread.join method, or by waiting for a Lock or Condition in the java.util.concurrent Library. In practice, the differece between the blocked and waiting state is not significant.
• Several methods have a timeout parameter. Calling them causes the thread to enter the timed waiting state. This state persists either until the timeout expired or the appropriate notification has been received. Methods with timeout include Thread.sleep and the timed versions of Object.wait, Thread.join, Lock.tryLock, and Condition.await.

A thread is terminated for one of two reasons:

• It dies a natural death because the run method exits normally.
• It dies abruptly because an uncaught exception terminates the run method.

In particular, you can kill a thread by invoking its stop method. That method throws a ThreadDeath error object that kills the thread. However, the stop method is deprecated, and you should never call is in yout own code.

一个线程在启动之后, 未必一直处于运行状态, 它什么时候处于运行状态取决于操作系统, 在某一时刻, 它可能处于非运行状态. 在程序逻辑中对此要特别小心, 防止因此产生错误. 例如, 如果你期望你的线程检测到某个具体时间点执行一个操作, 那么你的这个期望就有可能出现问题了, 因为也许在这个时间点到来的这个时刻, 刚好这个线程处于非运行状态, 此时你就无法捕捉这个时间点, 最终错过了执行操作的时机.

另外, 不要把这点与线程中调用sleep相混淆, 在线程中调用sleep之后, 线程就进入另一种状态, 而不是runnable状态. 我们在此处所说的是线程处于runnable状态时有可能处于非运行状态. 提到sleep方法, 还有一点要注意, 线程执行sleep(睡眠)到期自动苏醒并返回runnable状态后, 未必能够立即得到运行的机会. 因此, sleep方法中指定的时间是线程不会运行的最短时间, sleep方法不能保证该线程睡眠到期后就立即开始执行.

public void run() {
while(true) {
if(sum == 0) {
sum++;
}
......
}
......
}
sum为多线程共享的变量


public void run() {
while(true) {
synchronized(this) {
if(sum == 0) {
sum++;
}
}
......
}
......
}

04-23 386

03-10 960

01-17 555

06-26 3684

08-01 316

02-19 1464

#### C语言进阶_重点、难点与疑点解析 完整版.牟海军.pdf

2013年11月09日 25.04MB 下载

#### java线程状态转换图

2011年07月04日 69KB 下载

07-14 107

10-17 467