欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

线程状态及切换(三)

程序员文章站 2022-06-29 18:34:25
...

主要是实现线程各种执行状态

线程5中执行状态:

  • NEW:新建
  • RUNNABLE:运行
  • BLOCKED:线程阻塞并等待一个监听
  • WAITING:等待,无限等待知道一些特定操作
  • TIMED_WAITING:特定时间等待另外线程执行:计时等待
  • TERMINATED:线程结束

NEW

创建完线程后线程状态为NEW

   Runnable r = new Runnable() {
           @Override
           public void run() {
               System.out.println( Thread.currentThread().getName());
               try {
                   Thread.sleep(5000);

               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       };
       Thread thread = new Thread(r,"线程1");
       System.out.println("新建:"+thread.getState());//新建:NEW

RUNNABLE

线程执行start()

	   thread.start();
       System.out.println("开始执行:"+thread.getState());//开始执行:RUNNABLE

TIMED_WAITING

让线程计时等待 Thread.sleep(时间);

  public static void main(String[] args) throws InterruptedException {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                System.out.println( Thread.currentThread().getName());
                try {
                    Thread.sleep(5000);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };


        Thread thread = new Thread(r,"线程1");
        System.out.println("新建:"+thread.getState());
        thread.start();
        System.out.println("开始执行:"+thread.getState());

        int i=20;
        while (i>0){
            System.out.println(thread.getState());
            Thread.sleep(1000);
            i--;
        }

结果如图:
线程状态及切换(三)

TERMINATED

查看让面 当线程执行完以后线程结束

BLOCKED

我们写一个线程阻塞程序:让线程争抢资源不放导致阻塞

 private  static  final  Object r1 = new Object();
    private  static  final  Object r2 = new Object();

    public static void main(String[] args) throws InterruptedException {


        Thread t1 = new Thread(() -> {
            synchronized (r1) {
                System.out.println(Thread.currentThread() + "get r1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + "waiting r2");
                synchronized (r2) {
                    System.out.println(Thread.currentThread() + "get r2");

                }
            }

        }, "线程1");

        Thread t2 = new Thread(() -> {
            synchronized (r2) {
                System.out.println(Thread.currentThread() + "get r2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + "waiting r1");
                synchronized (r1) {
                    System.out.println(Thread.currentThread() + "get r1");

                }
            }

        }, "线程2");

        t1.start();
        t2.start();

        int i=20;
        while (i>0){
            System.out.println(t1.getState());
            Thread.sleep(1000);
            i--;
        }

    }

如图:
线程状态及切换(三)

WAITING

线程等待需要唤醒

public class MyWait {

    private volatile Boolean preIsA = false;

    synchronized void backA() {
        try {
            while (preIsA == true) {
                wait();
            }
            for (int i = 0; i < 5; i++) {
                System.out.println("*****");
            }
            preIsA = true;
            notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized void backB() {
        try {
            while (preIsA == false) {
                wait();
            }
            for (int i = 0; i < 5; i++) {
                System.out.println("#####");
            }
            preIsA = false;
            notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyWait myWait = new MyWait();
        for (int i = 0; i < 5; i++) {
            Thread A = new Thread(new Runnable() {
                @Override
                public void run() {
                    myWait.backA();
                }
            });
            Thread B = new Thread(() -> myWait.backB());
            A.start();
            B.start();


        }



    }
}