2016-10-26 7 views
4

私は次のように、スレッドダンプを取得するには、Androidのメーカーでツールを学んでいます:Javaスレッドの状態はどういう意味ですか?

get thread dump

私はこのようなすべてのスレッドの異なる状態に気づく、私は見ることができます

enter image description here

runnable,sleeping,waitingがある。深いスレッドスタック、このようなほとんどのスレッド・スタック、

"<61> [email protected]" daemon prio=5 waiting 
    java.lang.Thread.State: WAITING 
     at java.lang.Object.wait(Object.java:-1) 
     at java.lang.Thread.parkFor(Thread.java:1205) 
     at sun.misc.Unsafe.park(Unsafe.java:325) 
     at java.util.concurrent.locks.LockSupport.park(LockSupport.java:157) 
     at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2017) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1050) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:778) 
     at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1035) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1097) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:841) 

に私は、彼らがObject.waitですべての停止を行うことを混乱していると私は、しかしスレッドの状態はrunnablesleepingwaitingすることができますか?

これは、他の状態スレッドのスタックです。

RUNNABLE

<53> Rx[email protected]" daemon prio=5 runnable 
    java.lang.Thread.State: RUNNABLE 
     at java.lang.Object.wait(Object.java:-1) 
     at java.lang.Thread.parkFor(Thread.java:1205) 
     at sun.misc.Unsafe.park(Unsafe.java:325) 
     at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:197) 
     at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2056) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1062) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:778) 
     at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1035) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1097) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:841)</code> 

TIMED_WAITING

<58> [email protected]" daemon prio=5 sleeping 
    java.lang.Thread.State: TIMED_WAITING 
     at java.lang.Object.wait(Object.java:-1) 
     at java.lang.Thread.parkFor(Thread.java:1205) 
     at sun.misc.Unsafe.park(Unsafe.java:325) 
     at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:197) 
     at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2056) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1062) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:778) 
     at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1035) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1097) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:841) 
+1

、良いグーグルでスキルの問題です。 –

+0

回答を受け入れてください。私は特にこの質問について話しているわけではありませんが、私はあなたが良いが受け入れられない答えを持っているすべての質問について話しています。 –

答えて

4

はあなたがThreadクラスのインスタンスを作成する場合、スレッドが新しい状態であることを意味しますがstart()メソッドの呼び出しの前に:

Thread t = new Thread(...); // t is New 

Runnableをは、スレッドがの呼び出し後に実行可能状態であることを意味しstart()メソッド。基本的には:を実行

t.start(); // t is Runnable 

Runnableをの「サブ状態」である:スレッドスケジューラは、それを選択した場合にはスレッドが実行状態です。明示的に何もすることはできません。つまり、start()に電話してからお待ちください。

準備Runnableをの別の「サブ状態」である:スレッドがそれを選択するために、スレッドスケジューラのために実行し、待機の対象となります。

ブロックされたは、スレッドがまだ生きているが、現在実行できない状態を意味します。これは、たとえば、あるスレッドが​​ブロックを通過し、別のスレッドによって処理された場合に発生します。この場合、最初のスレッドはとブロックされ、になります。あなたのスレッドはをブロックしたり準備もされていないときを待っ

状態です。これは、通常、スレッドでwait()またはjoin()を呼び出すと発生します。

Thread t1 = new Thread(); // t1 is New 
Thread t2 = new Thread(); // t2 is New 
t1.start(); // t1 becomes Runnable 
t2.start(); // t2 becomes Runnable 
t1.join(); // t2 becomes Waiting, because t1 is processed until it terminates 

ほぼ同じものですが、sleep()を呼び出すことによって引き起こされ時限待機、と呼ばれる状態もあります。 wait(timeout)またはjoin(timeout)を呼び出すと、スレッドはの待機時間をに設定します。を終了した

Thread t = new Thread(); // t is New 
t.start(); // t is Runnable 
t.sleep(4000); // t get state of Timed Waiting for 4 seconds 

それはrun()メソッドが終了だときに終了または死んだ状態のスレッドです。

は、私はそれをすべてをカバーだと思う:)ここでは、より明確にそれを理解するための画像です:

Thread Life Cycle Java

JoxTraexが要求されたように、here'reソースは私が投稿する前にお読みください。

  1. javarush.ru
  2. http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html
  3. http://www.journaldev.com/1044/thread-life-cycle-in-java-thread-states-in-java

これは、スタックトレースのための `code`の代わりに画像を使用してください本当に...

+0

あなたはあなたのソースを掲示してください、そしてあなたはこの情報を得る場所を食べましたか? – JoxTraex

+1

@JoxTraexが私の答えを更新しました –

1
public static enum Thread.State 
extends Enum<Thread.State> 

スレッドの状態。スレッドは、次のいずれかの状態にあることができる:

NEW 
A thread that has not yet started is in this state. 

RUNNABLE 
A thread executing in the Java virtual machine is in this state. 

BLOCKED 
A thread that is blocked waiting for a monitor lock is in this state. 

WAITING 
A thread that is waiting indefinitely for another thread to perform a particular action is in this state. 

TIMED_WAITING 
A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. 

TERMINATED 
A thread that has exited is in this state. 

スレッドは、ある時点で唯一の状態であることができます。これらの状態は、オペレーティングシステムのスレッド状態を反映しない仮想マシン状態です。

詳細については、oracle's apiを参照してください。

関連する問題