割り込みスレッド()

公式サイトでは説明しました

このスレッドがの呼び出しでブロックされている場合  wait()、  wait(long)または  wait(long, int) 方法  Object 、クラス、またはの  join()、  join(long)、  join(long, int)、  sleep(long)、または  sleep(long, int)、このクラスのメソッドは、その割り込みステータスがクリアされ、それが受け取ることになります  InterruptedException

スレッドが中断することができますブロックされたときに、中断したがスローすることを意味InterruptedExceptionある例外待ち()、スリープ()、参加します()。

デモ睡眠は()中断され、

        スレッドT1 = 新しいスレッド(){
            @オーバーライド
            公共 のボイドの実行(){
                 ながら、){
                    System.out.println( "実行中..." );
                    してみてください{
                        Thread.sleep( 100L )。
                    } キャッチ(InterruptedExceptionある電子){
                        e.printStackTrace();
                        休憩;
                    }
                }
            }
        }。

        t1.start();
        Thread.sleep( 100L )。
        System.out.println(t1.isInterrupted())。
        t1.interrupt();
        System.out.println(t1.isInterrupted())。

睡眠中に()が異常に中断されたブレークを実行する、サイクルを壊し、それ以外の場合は、ループが実行されました

プレゼンテーション待ち()が中断され、

パッケージcom.dwz.concurrency.chapter6。

パブリック クラスThreadInterrupt {
     プライベート 静的 最終オブジェクトモニター= 新しいオブジェクト();
    パブリック 静的 ボイドメイン(文字列[]引数)がスローInterruptedExceptionあるが{
        スレッドT2 = 新しいスレッド(){
            @オーバーライド
            公共 のボイドの実行(){
                 ながら、){
                    System.out.println( "実行中..." );
                    同期(MONITOR){
                         試み{
                            MONITOR.wait( 100L )。
                        } キャッチ(InterruptedExceptionある電子){
                            System.out.println( "割り込み信号を受信..." );
                            e.printStackTrace();
                            休憩;
                        }
                    }
                }
            }
        }。

        t2.start();
        Thread.sleep( 100L )。
        System.out.println(t2.isInterrupted())。
        t2.interrupt();
        System.out.println(t2.isInterrupted())。
    }
}

例外は休憩を実行する際の睡眠1.()、のような、(待つ)が中断され、悪循環を断ち切る、そうでない場合は、ループが実行されました

2.wait()呼び出し側がオブジェクトでなければなりません

デモは中断され)(参加します

パッケージcom.dwz.concurrency.chapter6。

パブリック クラスThreadInterrupt3 {
     公共 静的 ボイドメイン(文字列[]引数)がスローInterruptedExceptionあるが{
        メインスレッド = にThread.currentThread()。
        スレッドT1 = 新しいスレッド(){
            @オーバーライド
            公共 のボイドの実行(){
                 ながら、){

             System.out.println(にThread.currentThread()のgetName()。)。
             System.err.println(にThread.currentThread()isInterruptedを()。)。

                }
            }
        }。

        t1.start();
        
        System.out.println( "メイン:" + main.getName());
        スレッドT2 = 新しいスレッド(){
            @オーバーライド
            公共 のボイドの実行(){
                 しようと{
                    Thread.sleep( 1000L )。
                } キャッチ(InterruptedExceptionある電子){
                    e.printStackTrace();
                }
                System.out.println( "T2:" + 。にThread.currentThread()のgetName())。
                main.interrupt();
                System.out.println( "割り込み..." );
            }
        }。
        t2.start();
        
        してみてください{
            t1.join();
        } キャッチ(InterruptedExceptionある電子){
            e.printStackTrace();
            t1.interrupt();
        }
        
        System.out.println( "メインisInterruptedを:" + main.isInterrupted());
        System.out.println( "isInterruptedをT1:" + t1.isInterrupted())。
        しばらく(!t1.isInterrupted()){
            Thread.sleep( 1000L )。
            System.out.println(t1.isInterrupted())。
        }
    }
}

1.私たちは)(別のスレッドでmain.interruptを実行するために、)(t1.join打破するためには、メインスレッドがオブジェクトである参加、知りたいです

2.isInterruptedは()のみmain.interrupt()メソッド、意味t1.join()を実行すると、スレッドは、t1はまだ破られていない中断され、中断されたかどうかを示し、t1.interrupt()が変更されますかT1状態

3. t1.join()(t1.startで実行されなければならない)とt2.start()の後。

おすすめ

転載: www.cnblogs.com/zheaven/p/12049998.html