Javaは、歩留まり、参加マルチスレッドのJavaを詳細睡眠法、歩留まり、詳細な睡眠法に参加マルチスレッディング

抜粋:https://www.cnblogs.com/happyone/p/11832110.html

Javaの詳細加入マルチスレッド化、歩留まり、睡眠方法

 

Javaのマルチスレッドプログラミングでは、Threadクラスは、コアと重要な役割の一つです。このように、使用されるいくつかの基本的な一般的な方法を理解するこのクラスは、コードをマルチスレッド開発や習熟度に基づいています。この章のデータは、いくつかの一般的な静的メソッドの意味と使用コードのスレッドをまとめました。

睡眠方法

ソースとして、次のとおりです。

    /**
     * Causes the currently executing thread to sleep (temporarily cease
     * execution) for the specified number of milliseconds, subject to
     * the precision and accuracy of system timers and schedulers. The thread
     * does not lose ownership of any monitors.
     *
     * @param  millis
     *         the length of time to sleep in milliseconds
     *
     * @throws  IllegalArgumentException
     *          if the value of {@code millis} is negative
     *
     * @throws InterruptedException * if any thread has interrupted the current thread. The * <i>interrupted status</i> of the current thread is * cleared when this exception is thrown. */ public static native void sleep(long millis) throws InterruptedException;

私たちは、それがローカルの方法であるため、局所的な睡眠は静的メソッドで見ることができ、実際には、Cライブラリ関数を達成するための基礎となる睡眠と呼ばれ、Javaコードを認識していませんでした。

何ミリ秒スリープを示すパラメータロングタイプがあります。

コメントを読んで意味睡眠方法は保証システムクロックとスケジューラの精度と正確さと、(一時的に実行を停止)指定されたミリ秒数をスリープタスクに現在実行中のスレッドを可能にすることです。しかし、スレッドは、それが保持しているロックを解放しません。

このメソッドが例外:InterruptedException割り込み例外をスローすることに注意してください。

睡眠はロックコードの例を解放しません。

public class ThreadsleepDemo{

    private Object object = new Object(); public static void main(String[] args) { ThreadsleepDemo threadsleepDemo = new ThreadsleepDemo(); Thread thread1 = threadsleepDemo.new SleepDemoThread(); thread1.setName("线程1"); Thread thread2 = threadsleepDemo.new SleepDemoThread(); thread2.setName("线程2"); thread1.start(); thread2.start(); } class SleepDemoThread extends Thread{ @Override public void run() { synchronized (object){ System.out.println(Thread.currentThread().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()); } } } } 

出力:

线程1开始运行
线程1运行结束
线程2开始运行
线程2运行结束

より多くの数回以上実行することができ、上記2の上方またはスレッド1とスレッドがあるかもしれない、それは別のスレッドに轢かが旅行を実行します常に、真ん中に来た実行中のスレッドを挿入しません。

利回り法

  /**
     * A hint to the scheduler that the current thread is willing to yield
     * its current use of a processor. The scheduler is free to ignore this
     * hint.
     *
     * <p> Yield is a heuristic attempt to improve relative progression
     * between threads that would otherwise over-utilise a CPU. Its use
     * should be combined with detailed profiling and benchmarking to
     * ensure that it actually has the desired effect.
     *
     * <p> It is rarely appropriate to use this method. It may be useful
     * for debugging or testing purposes, where it may help to reproduce
     * bugs due to race conditions. It may also be useful when designing
     * concurrency control constructs such as the ones in the
     * {@link java.util.concurrent.locks} package.
     */
    public static native void yield();

現在のスレッドスケジューラのヒントは、現在のCPUのアクチュエータを使用する権利を放棄する意向を表明したが、スケジューラはヒントを無視して自由です。

Yeildは、複数のスレッドの過度の使用であってもよいCPUとの間の相対的な進行を増強する仮の試みです。その使用は、詳細なパフォーマンス分析と組み合わせて、それが所望の効果を持っていることを確認するためにベンチマークする必要があります。

この方法ではほとんど使用されません。これは、デバッグやテストのために有用である可能性がある競合状態に基づいてエラーを再現するのを助けることができます。それはまた、(例えば、並列制御構造java.util.concurrent.locksパッケージとして)同時実行制御構造を設計するのに有用であり得ます。

メソッドに参加

3つの方法があり、オーバーロードに参加しています

join()
join(long millis)    
join(long millis,int nanoseconds)

第二の方法は、メインのソースfacieあります

    /**
     * Waits at most {@code millis} milliseconds for this thread to
     * die. A timeout of {@code 0} means to wait forever.
     *
     * <p> This implementation uses a loop of {@code this.wait} calls
     * conditioned on {@code this.isAlive}. As a thread terminates the * {@code this.notifyAll} method is invoked. It is recommended that * applications not use {@code wait}, {@code notify}, or * {@code notifyAll} on {@code Thread} instances. */ public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } }

スレッドが死ぬ前に指定されたミリ秒数を待っています。ノンパラメトリックな方法は、実際には永遠に待つ、ある参加(0)に参加するための呼び出しです。しかし、我々はソースコードを見ることができ、現在のスレッドがまだ生きているならば、それはwhileループで永遠に待つことになることを意味し、条件、すなわちのisAlive()メソッドがあります。

ビット無知、深めなければならない理解した上での例を見てみましょう。例えば、寝る前に、あなたはビブラート磨くしたいです

我々は完全にスレッドにブラシビブラートを働きます。

public class ScanDouyin extends Thread{ // 浏览抖音的时长 private int scanTime; public ScanDouyin(String name, int scanTime){ super(name); scanTime = this.scanTime; } @Override public void run() { System.out.println(getName() + ":开始刷抖音了"); try { // 刷抖音的时间 sleep(scanTime); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(getName() +":抖音刷完了,睡觉吧"); } }

ここで、スリープスレッドへの準備ができています

/**
 *  准备睡觉了,睡前想要刷个抖音
 */
public class ReadySleep extends Thread{ private ScanDouyin scanDouyin; public ReadySleep(String name,ScanDouyin scanDouyin){ super(name); this.scanDouyin = scanDouyin; } @Override public void run() { System.out.println(getName() + ":准备开始睡觉啦"); try { // 睡前刷把抖音 scanDouyin.join(); // 准备睡觉的具体内容 System.out.println("开始睡觉"); sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(getName() + ":zzzzzzzz,已经睡着了"); } public static void main(String[] args) { ScanDouyin scanDouyin = new ScanDouyin("刷抖音线程",10000); ReadySleep readySleep = new ReadySleep("睡觉线程",scanDouyin); readySleep.start(); scanDouyin.start(); } }

出力:

睡觉线程:准备开始睡觉啦
刷抖音线程:开始刷抖音了
刷抖音线程:抖音刷完了,睡觉吧
开始睡觉
睡觉线程:zzzzzzzz,已经睡着了

ここでは、私のビブラート設定時間を磨く10秒で、スレッドを寝実行時間は0.1秒で100ミリ秒、です。

(ブラシスレッドが終了したビブラート、スレッドの終焉)はビブラートブラッシング後まで待たなければなりません睡眠スレッドを作る、スリープ状態に始めることができ、スリープ状態にスレッド内のスレッドビブラートブラシ法に参加するための呼び出しとして見ることができます。

この時点で、ということが理解されるべき対象スレッドTの端部が別のスレッドTに(偽すなわちt.isAlive()メソッドの戻り)回復されるまでt.joinスレッドコール()は、このスレッドが中断される場合。

マイクロチャンネル公衆数の大隊の司令官で懸念|大隊の司令官2冊のノートには、秘密の大隊の司令官のJava技術情報を受け取るために「2大隊の司令官」を返信。
 

Javaのマルチスレッドプログラミングでは、Threadクラスは、コアと重要な役割の一つです。このように、使用されるいくつかの基本的な一般的な方法を理解するこのクラスは、コードをマルチスレッド開発や習熟度に基づいています。この章のデータは、いくつかの一般的な静的メソッドの意味と使用コードのスレッドをまとめました。

睡眠方法

ソースとして、次のとおりです。

    /**
     * Causes the currently executing thread to sleep (temporarily cease
     * execution) for the specified number of milliseconds, subject to
     * the precision and accuracy of system timers and schedulers. The thread
     * does not lose ownership of any monitors.
     *
     * @param  millis
     *         the length of time to sleep in milliseconds
     *
     * @throws  IllegalArgumentException
     *          if the value of {@code millis} is negative
     *
     * @throws InterruptedException * if any thread has interrupted the current thread. The * <i>interrupted status</i> of the current thread is * cleared when this exception is thrown. */ public static native void sleep(long millis) throws InterruptedException;

私たちは、それがローカルの方法であるため、局所的な睡眠は静的メソッドで見ることができ、実際には、Cライブラリ関数を達成するための基礎となる睡眠と呼ばれ、Javaコードを認識していませんでした。

何ミリ秒スリープを示すパラメータロングタイプがあります。

コメントを読んで意味睡眠方法は保証システムクロックとスケジューラの精度と正確さと、(一時的に実行を停止)指定されたミリ秒数をスリープタスクに現在実行中のスレッドを可能にすることです。しかし、スレッドは、それが保持しているロックを解放しません。

このメソッドが例外:InterruptedException割り込み例外をスローすることに注意してください。

睡眠はロックコードの例を解放しません。

public class ThreadsleepDemo{

    private Object object = new Object(); public static void main(String[] args) { ThreadsleepDemo threadsleepDemo = new ThreadsleepDemo(); Thread thread1 = threadsleepDemo.new SleepDemoThread(); thread1.setName("线程1"); Thread thread2 = threadsleepDemo.new SleepDemoThread(); thread2.setName("线程2"); thread1.start(); thread2.start(); } class SleepDemoThread extends Thread{ @Override public void run() { synchronized (object){ System.out.println(Thread.currentThread().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()); } } } } 

出力:

线程1开始运行
线程1运行结束
线程2开始运行
线程2运行结束

より多くの数回以上実行することができ、上記2の上方またはスレッド1とスレッドがあるかもしれない、それは別のスレッドに轢かが旅行を実行します常に、真ん中に来た実行中のスレッドを挿入しません。

利回り法

  /**
     * A hint to the scheduler that the current thread is willing to yield
     * its current use of a processor. The scheduler is free to ignore this
     * hint.
     *
     * <p> Yield is a heuristic attempt to improve relative progression
     * between threads that would otherwise over-utilise a CPU. Its use
     * should be combined with detailed profiling and benchmarking to
     * ensure that it actually has the desired effect.
     *
     * <p> It is rarely appropriate to use this method. It may be useful
     * for debugging or testing purposes, where it may help to reproduce
     * bugs due to race conditions. It may also be useful when designing
     * concurrency control constructs such as the ones in the
     * {@link java.util.concurrent.locks} package.
     */
    public static native void yield();

現在のスレッドスケジューラのヒントは、現在のCPUのアクチュエータを使用する権利を放棄する意向を表明したが、スケジューラはヒントを無視して自由です。

Yeildは、複数のスレッドの過度の使用であってもよいCPUとの間の相対的な進行を増強する仮の試みです。その使用は、詳細なパフォーマンス分析と組み合わせて、それが所望の効果を持っていることを確認するためにベンチマークする必要があります。

この方法ではほとんど使用されません。これは、デバッグやテストのために有用である可能性がある競合状態に基づいてエラーを再現するのを助けることができます。それはまた、(例えば、並列制御構造java.util.concurrent.locksパッケージとして)同時実行制御構造を設計するのに有用であり得ます。

メソッドに参加

3つの方法があり、オーバーロードに参加しています

join()
join(long millis)    
join(long millis,int nanoseconds)

第二の方法は、メインのソースfacieあります

    /**
     * Waits at most {@code millis} milliseconds for this thread to
     * die. A timeout of {@code 0} means to wait forever.
     *
     * <p> This implementation uses a loop of {@code this.wait} calls
     * conditioned on {@code this.isAlive}. As a thread terminates the * {@code this.notifyAll} method is invoked. It is recommended that * applications not use {@code wait}, {@code notify}, or * {@code notifyAll} on {@code Thread} instances. */ public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } }

スレッドが死ぬ前に指定されたミリ秒数を待っています。ノンパラメトリックな方法は、実際には永遠に待つ、ある参加(0)に参加するための呼び出しです。しかし、我々はソースコードを見ることができ、現在のスレッドがまだ生きているならば、それはwhileループで永遠に待つことになることを意味し、条件、すなわちのisAlive()メソッドがあります。

ビット無知、深めなければならない理解した上での例を見てみましょう。例えば、寝る前に、あなたはビブラート磨くしたいです

我々は完全にスレッドにブラシビブラートを働きます。

public class ScanDouyin extends Thread{ // 浏览抖音的时长 private int scanTime; public ScanDouyin(String name, int scanTime){ super(name); scanTime = this.scanTime; } @Override public void run() { System.out.println(getName() + ":开始刷抖音了"); try { // 刷抖音的时间 sleep(scanTime); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(getName() +":抖音刷完了,睡觉吧"); } }

ここで、スリープスレッドへの準備ができています

/**
 *  准备睡觉了,睡前想要刷个抖音
 */
public class ReadySleep extends Thread{ private ScanDouyin scanDouyin; public ReadySleep(String name,ScanDouyin scanDouyin){ super(name); this.scanDouyin = scanDouyin; } @Override public void run() { System.out.println(getName() + ":准备开始睡觉啦"); try { // 睡前刷把抖音 scanDouyin.join(); // 准备睡觉的具体内容 System.out.println("开始睡觉"); sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(getName() + ":zzzzzzzz,已经睡着了"); } public static void main(String[] args) { ScanDouyin scanDouyin = new ScanDouyin("刷抖音线程",10000); ReadySleep readySleep = new ReadySleep("睡觉线程",scanDouyin); readySleep.start(); scanDouyin.start(); } }

出力:

睡觉线程:准备开始睡觉啦
刷抖音线程:开始刷抖音了
刷抖音线程:抖音刷完了,睡觉吧
开始睡觉
睡觉线程:zzzzzzzz,已经睡着了

ここでは、私のビブラート設定時間を磨く10秒で、スレッドを寝実行時間は0.1秒で100ミリ秒、です。

(ブラシスレッドが終了したビブラート、スレッドの終焉)はビブラートブラッシング後まで待たなければなりません睡眠スレッドを作る、スリープ状態に始めることができ、スリープ状態にスレッド内のスレッドビブラートブラシ法に参加するための呼び出しとして見ることができます。

この時点で、ということが理解されるべき対象スレッドTの端部が別のスレッドTに(偽すなわちt.isAlive()メソッドの戻り)回復されるまでt.joinスレッドコール()は、このスレッドが中断される場合。

おすすめ

転載: www.cnblogs.com/xichji/p/11835736.html