美文网首页
关于线程的几个方法研究

关于线程的几个方法研究

作者: 最美下雨天 | 来源:发表于2019-07-11 09:08 被阅读0次

1、关于线程的终止
stop方法

public class ThreadTest {
    /**
     * 关于线程的几个方法的演示
     * 线程中断:interrupt、stop
     * join、yield、wait
     * @param args
     */
    static int sum=0;
    static Object object=new Object();
    static boolean isRun=true;
    public static void main(String args[])
    {

        Thread consumer=new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRun)
                {
                    synchronized (object)
                    {
                        if(sum>0)
                        {
                            sum--;
                            System.out.println("消费者线程进入============运行状态。。。"+sum);
                            //不要直接写notify
                            object.notifyAll();
                        }
                        else
                        {
                            try {
                                System.out.println("消费者线程进入等待状态。。。");
                                object.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        });
        consumer.start();


        Thread product=new Thread(new Runnable() {
            @Override
            public void run() {

                while (isRun)
                {
                    synchronized (object)
                    {
                        if(sum>=10)
                        {
                            try {
                                System.out.println("生产者线程进入等待状态。。。");
                                object.wait();
                            } catch (InterruptedException e) {

                                e.printStackTrace();
                            }
                        }
                        else
                        {
                            sum++;
                            System.out.println("生产者线程进入============运行状态。。。"+sum);
                            object.notifyAll();
                        }
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        product.start();


    }
}


看运行结果:
我们规定商品的数量不可能超过10,否则会产生严重后果


image.png

生产者消费者有条不紊的进行着

但是,如果我们随意使用了stop方法,我们在最后面加上stop方法

public class ThreadTest {
    /**
     * 关于线程的几个方法的演示
     * 线程中断:interrupt、stop
     * join、yield、wait
     * @param args
     */
    static int sum=0;
    static Object object=new Object();
    static boolean isRun=true;
    public static void main(String args[])
    {

        Thread consumer=new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRun)
                {
                    synchronized (object)
                    {
                        if(sum>0)
                        {
                            sum--;
                            System.out.println("消费者线程进入============运行状态。。。"+sum);
                            //不要直接写notify
                            object.notifyAll();
                        }
                        else
                        {
                            try {
                                System.out.println("消费者线程进入等待状态。。。");
                                object.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        });
        consumer.start();


        Thread product=new Thread(new Runnable() {
            @Override
            public void run() {

                while (isRun)
                {
                    synchronized (object)
                    {
                        if(sum>=10)
                        {
                            try {
                                System.out.println("生产者线程进入等待状态。。。");
                                object.wait();
                            } catch (InterruptedException e) {

                                e.printStackTrace();
                            }
                        }
                        else
                        {
                            sum++;
                            System.out.println("生产者线程进入============运行状态。。。"+sum);
                            object.notifyAll();
                        }
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        product.start();


        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //终止生产者线程
        product.stop();

    }
}

运行:


image.png

生产者stop了,消费者进入了永远的阻塞状态
根据stop方法的注释

This method is inherently unsafe.  Stopping a thread with
     *       Thread.stop causes it to unlock all of the monitors that it
     *       has locked 

就是说这个方法是不安全的,调用stop方法后,会释放它持有的锁
,而且在调用stop方法的时候,线程正在执行哪块代码是不确定的,所以有了替代方法interrupt

 //终止生产者线程
        //注意特殊情况:
        //1.执行interrupt的时候product线程正在sleep或者正在wait,那么product线程会抛出一个InterruptedException异常,并且clear中断状态
        //此时如果用isInterrupted()检测,那么返回的还是false

        product.interrupt();
public class ThreadTest {
    /**
     * 关于线程的几个方法的演示
     * 线程中断:interrupt、stop
     * join、yield、wait
     * @param args
     */
    static int sum=0;
    static Object object=new Object();
    static boolean isRun=true;
    public static void main(String args[])
    {

        Thread consumer=new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRun)
                {
                    synchronized (object)
                    {
                        if(sum>0)
                        {
                            sum--;
                            System.out.println("消费者线程进入============运行状态。。。"+sum);
                            //不要直接写notify
                            object.notifyAll();
                        }
                        else
                        {
                            try {
                                System.out.println("消费者线程进入等待状态。。。");
                                object.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        });
        consumer.start();


        Thread product=new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRun)
                {
                    if(Thread.currentThread().isInterrupted())
                    {
                        System.out.println("----------------");
                        //线程处于终端状态了
                        isRun=false;
                        //清理资源
                    }
                    synchronized (object)
                    {
                        if(sum>=10)
                        {
                            try {
                                System.out.println("生产者线程进入等待状态。。。");
                                object.wait();
                            } catch (InterruptedException e) {
                                isRun=false;
                                e.printStackTrace();
                            }
                        }
                        else
                        {
                            sum++;
                            System.out.println("生产者线程进入============运行状态。。。"+sum);
                            object.notifyAll();
                        }
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        isRun=false;
                        e.printStackTrace();
                    }
                }

            }
        });
        product.start();


        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //终止生产者线程
        //注意特殊情况:
        //1.执行interrupt的时候product线程正在sleep或者正在wait,那么product线程会抛出一个InterruptedException异常,并且clear中断状态
        //此时如果用isInterrupted()检测,那么返回的还是false

        product.interrupt();

    }
}

运行:


image.png

相关文章

  • 关于线程的几个方法研究

    1、关于线程的终止stop方法 看运行结果:我们规定商品的数量不可能超过10,否则会产生严重后果 生产者消费者有条...

  • 线程中断Thread的interrupt()方法

    关于interrupt()方法的介绍 本线程中断自己是被允许的;其它线程调用本线程的interrupt()方法时,...

  • Golang 中 runtime 的使用

    runtime 调度器是个非常有用的东西,关于 runtime 包几个方法: Gosched:让当前线程让出 cp...

  • RxJava系列_02线程调度

    1、关于线程调度的例子, 就只用过几个操作符, 所以只针对这几个操作符进行源码阅读; 2、关于线程调度, 有下面几...

  • Java多线程中synchronized关键字的使用方法

    关于多线程Thread方法的几点总结, isAlive方法判断线程是否存活 ,wait方法传入参数0无限期等待 y...

  • 【Java进阶营】Java多线程中synchronized关键字

    关于多线程Thread方法的几点总结, isAlive方法判断线程是否存活 ,wait方法传入参数0无限期等待yi...

  • Java锁

    Thread几个方法 a、start()方法,调用该方法开始执行该线程;b、stop()方法,调用该方法强制结束该...

  • JUC(11) - 线程池

    11. 线程池 第四种获取线程的方法:线程池,一个 ExecutorService,它使用可能的几个池线程之一执行...

  • Java 多线程

    1 多线程 1.1 多线程介绍   学习多线程之前,我们先要了解几个关于多线程有关的概念。  进程:进程指正在运行...

  • 自带线程池

    关于线程和线程池的学习,我们可以从以下几个方面入手: 第一,什么是线程,线程和进程的区别是什么 第二,线程中的基本...

网友评论

      本文标题:关于线程的几个方法研究

      本文链接:https://www.haomeiwen.com/subject/omncqqtx.html