GitHubのプログラム猿協会は、歓迎スター
https://github.com/ITfqyd/cxyxsを
対応するトピックを形成するために、この記事では、githubのに記録されています。
記事のディレクトリ
序文
聞いたが、我々はすでにマルチスレッドの一般的な理解を持って、以前の記事をお読みください。例外なく>ユースはもちろん、スレッド - >青少年 - 世界のすべては、人々は、ライフサイクル、幼児を持って、秩序あります。社長から行くために、病気や死のスレッドを見てみましょう従ってください。
1.ソースコードを見ることで、スレッドのライフサイクルを理解します
まず、最初のノックスレッド、ソースを表示クラスには、Altキー+ 7(アイデア)の方法により、すべてのクラスを確認してください。
このクラスは、我々は、列挙クラスの状態を見つけることができます。
/**
* A thread state. A thread can be in one of the following states:
* <ul>
* <li>{@link #NEW}<br>
* A thread that has not yet started is in this state.
* </li>
* <li>{@link #RUNNABLE}<br>
* A thread executing in the Java virtual machine is in this state.
* </li>
* <li>{@link #BLOCKED}<br>
* A thread that is blocked waiting for a monitor lock
* is in this state.
* </li>
* <li>{@link #WAITING}<br>
* A thread that is waiting indefinitely for another thread to
* perform a particular action is in this state.
* </li>
* <li>{@link #TIMED_WAITING}<br>
* A thread that is waiting for another thread to perform an action
* for up to a specified waiting time is in this state.
* </li>
* <li>{@link #TERMINATED}<br>
* A thread that has exited is in this state.
* </li>
* </ul>
*
* <p>
* A thread can be in only one state at a given point in time.
* These states are virtual machine states which do not reflect
* any operating system thread states.
*
* @since 1.5
* @see #getState
*/
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
/**
* Returns the state of this thread.
* This method is designed for use in monitoring of the system state,
* not for synchronization control.
*
* @return this thread's state.
* @since 1.5
*/
public State getState() {
// get current thread state
return sun.misc.VM.toThreadState(threadStatus);
}
getStateをメソッドは、現在のスレッドの状態を取得します。
この列挙クラスを通じて、我々は見ることができる6種のライフサイクルのスレッド
- NEW(新)
- RUNNABLE(レディ)
- BLOCKED(ブロッキング)
- WAITING(待機)
- TIMED_WAITING(リミット待機)
- TERMINATED(死亡または完全な)
スレッド理解サイクル状態の2例6種類。
NEW(新)
ただ、スレッドを作成し、実行していない、それはスレッドの新しい状態です。
public class New {
public static void main(String[] args) {
Thread t = new Thread();
System.out.println(t.getState());
}
}
RUNNABLE(レディ)
スレッドのstartメソッドが呼び出された後、彼はJVMを実行されていると述べ、CPUリソースはまだ予定されていません。それを呼び出すする準備ができました。
package com.cxyxs.thread.five;
/**
* Description:转发请注明来源 程序猿学社 - https://ithub.blog.csdn.net/
* Author: 程序猿学社
* Date: 2020/2/22 11:08
* Modified By:
*/
public class RUNNABLE {
public static void main(String[] args) {
Thread t = new Thread();
t.start();
System.out.println(t.getState());
}
}
TERMINATED(死亡または完全な)
package com.cxyxs.thread.five;
/**
* Description:转发请注明来源 程序猿学社 - https://ithub.blog.csdn.net/
* Author: 程序猿学社
* Date: 2020/2/22 11:24
* Modified By:
*/
public class TERMINATED {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread();
t.start();
Thread.sleep(5000);
System.out.println(t.getState());
}
}
コードと組み合わせることで、結果がレディ状態を実行可能な、あなたのロータリアン、なぜ私は感じていますか増加のThread.sleep後、ステータスコードは、わずかに変更されますか?
最初の答えこの質問の前に、我々は、タイムスライスの概念を理解する必要があり
、1つのCPUのみ、1チャネルを仮定すると、すぐに実行されていないスレッドを呼び出した後ではなく、リソーススケジューリングのCPUによって私たちの方法を開始し、複数のスレッドが同時に存在しています同時に、グラブリソースへの唯一のスレッドが存在する可能性があるため、CPUは、このスレッドを与える割り当てられたタイムスライスを一定時間内に、公正かつ公平な方法を確保するために、処理されていない場合、CPUは、固定され、糸をカットします時間処理し、糸をカットします。これは資源の無駄ではありません。
-スタートの後、私は5秒遅れた、スレッドはとても素晴らしい結果が終了した、レースを終えたことを確認することです
BLOCKED(ブロッキング)
現在のスレッドがモニターロック処理を待っています。あるいは、例えば、目詰まり状態のスレッドを引き起こし、ブロック同期メソッドを同期実行します。
package com.cxyxs.thread.five;
/**
* Description:转发请注明来源 程序猿学社 - https://ithub.blog.csdn.net/
* Author: 程序猿学社
* Date: 2020/2/22 14:13
* Modified By:
*/
public class BLOCKED {
public static synchronized void test() throws Exception{
//模拟业务
Thread.sleep(3000);
}
public static void main(String[] args) throws Exception{
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
test();
} catch (Exception e) {
e.printStackTrace();
}
}
};
Thread thread = new Thread(runnable);
thread.start();
Thread thread1= new Thread(runnable);
thread1.start();
Thread.sleep(100);
System.out.println(thread1.getState());
}
}
二つの重要な段階上のコード
- 最初のステップの後、ロックされたときに複数のスレッドへのアクセスに相当します同期方法を、開始、あなたは資格がアクセスをアクセスするためにロックを取得する必要があります。
- 第二段階の休眠100ミリ秒で、このコードは、スレッドが稼働していることを確認することです、非常に重要です。
ケース:
小さなピット王の隣に、王はルのピットに直接の後に行くがロックされていた、他の人、入るために、使用後に王を待つ必要。ここにプラグインする他の人々 。
いくつかの方法は、ブロッキングにつながります
- 同期ブロックまたは同期コードの方法
- Object.wait
WAITING(待機)
スレッドが別のスレッドを待って目覚め、この状態で待機することです。
package com.cxyxs.thread.five;
/**
* Description:转发请注明来源 程序猿学社 - https://ithub.blog.csdn.net/
* Author: 程序猿学社
* Date: 2020/2/22 14:46
* Modified By:
*/
public class WAITING {
public static void main(String[] args) throws Exception{
Object locker = new Object();
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
synchronized (locker){
System.out.println("调用wait");
locker.wait();
System.out.println("调用wait后");
}
System.out.println("调用wait业务");
} catch (Exception e) {
e.printStackTrace();
}
}
};
Thread t = new Thread(runnable);
t.start();
Thread.sleep(100);
runnable = new Runnable() {
@Override
public void run() {
try {
synchronized (locker){
System.out.println("调用notify");
locker.notify();
System.out.println("调用notify后");
}
System.out.println("调用notif业务");
} catch (Exception e) {
e.printStackTrace();
}
}
};
Thread t1 = new Thread(runnable);
t1.start();
System.out.println(t.getState()+","+t1.getState());
}
}
スレッドTウェイクによってT1スレッドは、スリープ100ミリ秒は、スレッドtの待ち時間を呼び出し、すでに実行されているスレッドを確保するために、スレッドは、スレッドが待ちキュー、tはここでダウンを実行されていない目詰まり現在のスレッド、追加の必要性に入ることを示していますスレッドが彼を目覚め、同期ロックが。ロックを取得した後、呼び出しは、通知スレッドtを覚ます、ロッカーである
少数のWAITINGにつながる可能性
- Object.wait
- Thread.join
- LockSupport.parkは
Object.notifyによって起こされました
TIMED_WAITING(リミット待機)
別のスレッドが完了するまでの時間の特定の期間内のスレッドが待機状態、自動ウェイクアップ、待機する限られた時間と呼ばがあります。
package com.cxyxs.thread.five;
/**
* Description:转发请注明来源 程序猿学社 - https://ithub.blog.csdn.net/
* Author: 程序猿学社
* Date: 2020/2/22 13:42
* Modified By:
*/
public class TIMEDWAITING{
public static void main(String[] args) throws Exception{
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
//1
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread thread = new Thread(runnable);
thread.start();
//2
Thread.sleep(100);
System.out.println(thread.getState());
}
}
最初のステップ、5秒の遅延は、単にリソースをできるように
第二段階、100ミリ秒遅れて、そのスレッドが既に状態を実行していることを確認することです。そして、このスレッドは、タイムスライスをつかむためにことを確認します。
ケース:
ファラオは、バスを待って、仕事に行くつもり隣、長い時間を待っていたが、何の座席、思考、または電車に乗ると、最終的には、次の電車を待って、会社に行きませんでしたが見つかりませんでした。
資格は、他の人々を与える上で、彼は車を入れるので実際には、私はCPUタイムスライスリソースの資格の内側に持っているものが、理由は非常に怠惰な学生、の、私は、少し不快たいです。もちろん、最後に、エイミーはトラート会社によくやりました。
いくつかの方法が、以下の制限待ちにつながることができます
- Thread.sleep
- Object.wait
- Thread.join
- LockSupport.parkNanos
- LockSupport.parkUntil
シックス概要を述べて
インタビューは、二つのグラフから話を始めることができ、尋ねライフサイクルに会いました。起動するには、次のチャートを起動し、最初の図。そもそも大きな方向で起動し、基本となるソースコードを達成する方法です。
追伸
次の記事では、デモ実現スレッドがいくつかの一般的に使用される方法を理解して結合します。