基本的なスレッドAPI

スレッド譲歩
Thread.yield(); //状態を実行- >レディ状態

public class ThreadYield {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }).start();
        while(Thread.activeCount()>1){//使用调试的方法运行
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName());
    }
}

第二に、スレッド待ち
(1)参加

public static void main(String[] args) throws InterruptedException {
   Thread t= new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName());
        }
    });
    t.start();
    //先将T线程执行完毕,再往下执行
    t.join();
    t.join(2000);
    System.out.println(Thread.currentThread().getName());
}

activeCount()+収率()結合、(2)使用

public class ThreadYield {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }).start();
        while(Thread.activeCount()>1){
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName());
    }
}

第三に、スレッドが中断された
(1)isInterruptedを()
I nterrupted(2)()
本当の直接割り込みではありませんが、スレッドは、独自で決定される中断する、具体的かどうか、中断されるスレッドのニーズを伝えるために
(3)割り込み()
(4)有効期限が切れ方法停止();

注:1.线程启动以后:中断标志位=false
    2.在线程运行态中,处理线程中断,需要自行通过判断中断标志位,来进行中断的处理逻辑。通过方法thread.isInterrupted()/Thread.interrupted()
    3.线程因调用wait()/join()/sleep()处于阻塞状态时,将线程中断,会造成:
	 (1)在这三个阻塞方法所在的代码行,直接抛出InterruptedException异常
	 (2)抛出异常之后,重置线程的中断标志位(=true)
    4.static void interrupted():返回中断标志位,并重置标志位
      void isInterrupted():返回中断标志位
    5.自定义的标志位满足不了线程处于阻塞状态时,中断操作


public class InterruptThread {
    //中断一个线程,但是线程没有处理中断
    public static void test1(){
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){

                }
            }
        });
        t.start();
        t.interrupt();
    }
    //
    public  static  void test2() throws InterruptedException {
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {//线程运行状态下处理中断标志
                for(int i=0;i<50;i++){
                    System.out.println(i+""+Thread.currentThread().isInterrupted());
                }
//              while (!Thread.currentThread().isInterrupted()){
                while (!Thread.interrupted()){
                  System.out.println(Thread.currentThread().getName());
              }
            }
        });
        t.start();//t线程中的中断标志位=false
        t.interrupt();//t线程中的中断标志位=true
    }
    public  static  void test3() throws InterruptedException {
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().isInterrupted());//true
                    //线程处于调用wait()/join()/sleep()阻塞的时候,如果把当前线程中断,会直接抛出一个异常
                    //阻塞状态时,通过捕获及处理异常,来处理线程的中断逻辑
                    //抛出异常以后线程的标志位被重置
                    Thread.sleep(9999999);
                    System.out.println(Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println(Thread.currentThread().isInterrupted());//false
                }

            }
        });
        t.start();//t线程中的中断标志位=false
        t.interrupt();//t线程中的中断标志位=true
    }
    public  static  void test4() throws InterruptedException {
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
               for (int i=0;i<10;i++){
                   System.out.println(Thread.currentThread().isInterrupted());
                  // System.out.println(Thread.interrupted());//返回中断标志位,并重置标志位
               }

            }
        });
        t.start();//t线程中的中断标志位=false
        t.interrupt();//t线程中的中断标志位=true
    }
    private  static volatile boolean IS_INTERRUPTDE;
    //使用自定义的中断标志位
    public static  void test5(){
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
                //自定义的标志位 能满足线程处于运行态的中断操作
//                while (!IS_INTERRUPTDE){
//                    System.out.println(Thread.currentThread().getName());
//                }
                //自定义的标志位满足不了西安城处于阻塞状态时,中断操作
                try {
                    Thread.sleep(99999);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        IS_INTERRUPTDE=true;
    }
    public static void main(String[] args) throws InterruptedException {
        //test1();
        //test2();
        // test3();
        // test4();
        test5();
    }
}

第四に、デーモンスレッド

少なくとも1つの非デーモンスレッドが破壊されない、プロセスから撤退しないであろう
非デーモンスレッドは、一般労働者と呼ぶことができ、それがデーモンスレッドバックグラウンドスレッドと呼ぶことができます

public class DaemonThread {
    public static void main(String[] args) {
         Thread t=new Thread(new Runnable() {
             @Override
             public void run() {
                 try {
                     Thread.sleep(99999999L);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
             }
         });
         //设置线程为守护线程
         t.setDaemon(true);
         t.start();
    }
}

第五に、スレッド開始
、実行()とstart()

MyThread myThread=new MyThread();
myThread.start();
//run 方法直接调用,不会启动现线程,只会在当前main线程中调用run方法
//myThread.run();
公開された71元の記事 ウォンの賞賛3 ビュー1037

おすすめ

転載: blog.csdn.net/qq_44262984/article/details/105164072