线程笔记1

1,守护线程 daemon,join方法初识:

简介
守护线程-deamon:在main线程标记子线程为守护线程,则无论子线程是否执行结束,main线程结束,则子线程为守护线程的也跟着结束;
join:等待此子线程执行结束,才会执行父线程的剩余业务
1>案例1 deamon,join使用
public class Chap01 {

   /***
        *  t2 跑完了,才会继续跑content3的内容;
    *  t3 跑完了,t1就不再等待,随main结束而结束
    */
    public static void main(String[] args) {
        //---- content1  -----
        Thread t1 =new Thread(()->{
            try {
                System.out.println("t1 is run");
                Thread.sleep(10_000); //睡10s
                System.out.println("t1 is end");
            } catch (InterruptedException e) {
                System.out.println("t1 InterruptedException");
                e.printStackTrace();
            }
        });
        //将此线程标记为daemon线程或用户线程。
        //当运行的唯一线程都是守护进程线程时,Java虚拟机退出。
        t1.setDaemon(true); //跟随主线程一起结束,设置子线程的守护线程是main线程
        t1.start();
        System.out.println("main is wait");
        //---- content2  -----
        Thread t2 =new Thread(()->{
            try {
                System.out.println("t2 is run");

                Thread.sleep(2_000);
                System.out.println("t2 is end");
            } catch (InterruptedException e) {
                System.out.println("t2 InterruptedException");
                e.printStackTrace();
            }
        });

        t2.start();
        try {
            //如果有线程中断了当前线程。抛出此异常时,当前线程的中断状态将被清除。
            t2.join();  //直到t2线程跑完了,才会继续执行主线程的信息
        } catch (InterruptedException e) {
            System.out.println("main InterruptedException");
            e.printStackTrace();
        }
        //---- content3  -----
        System.out.println("main is end");
    }
}

2> 案例2  中断线程 - interrupt

public class Chap2 {

    public static void main(String[] args) {
        Thread t1 =new Thread(()->{
            System.out.println("t1 is run");
            while(true) {
                try {

                    System.out.println("t1 thread:" + Thread.currentThread().isInterrupted());
                    Thread.sleep(20_000);
                } catch (InterruptedException e) {
                    System.out.println("t1 thread get InterruptedException:" + Thread.currentThread().isInterrupted());
                    e.printStackTrace();
                }
                
            }
        System.out.println("t1 is end");

        });
        Thread main= Thread.currentThread();
        Thread t2 =new Thread(()->{
            try {
                System.out.println("t2 is runing");
                Thread.sleep(3_000);
                System.out.println("t2 is ending");
                main.interrupt();
            } catch (InterruptedException e) {
                System.out.println("t2 thread get InterruptedException:" + Thread.currentThread().isInterrupted());
                e.printStackTrace();
            }
        });
        t1.setDaemon(true);
        t1.start();
        t2.start();
//        t1.interrupt();
        try {
            t1.join();
        } catch (InterruptedException e) {
            System.out.println("t1 thread join 被打断...");
            e.printStackTrace();
        }

        Thread.currentThread().interrupt();
        System.out.println("main ...end");
    }
}

3> 案例3 避免死循环(死锁)

 代码层控制, 中断 阻塞线程

====================避免死锁1=================

public class Chap3 {

    public static void main(String[] args) {

        Student student=new Student();
        Thread stuRun=new Thread(student);
        stuRun.start();

        try {
            Thread.sleep(3_000);

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

        System.out.println("end end...");
    }
}


class Student implements Runnable{

    public boolean flag=true;
    @Override
    public void run() {
        while(true){
            System.out.println("this is in studing");
            try {
                if(Thread.currentThread().isInterrupted()){
                    break;//如果线程被中断
                }
                Thread.sleep(1_000);
                
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

=======================避免死锁2================

public class ExecuteThread {
    public Thread executeThread;
    private boolean isFinished=false;

    public void exeThread(Runnable task){
        executeThread = new Thread(()->{
            Thread run =new Thread(task);
            run.setDaemon(true);
            run.start();
            try {
                run.join();
                isFinished=true;
            } catch (InterruptedException e) {
                System.out.println("防止阻塞,线程异常结束");
                e.printStackTrace();
            }
        }
        );
        executeThread.start();
    }
    //设置再调用多长时间 后结束任务
    public void stopThread(long minuts){ 

        long startMinut=System.currentTimeMillis();
        while(!isFinished){ //线程为执行结束
            if(System.currentTimeMillis()-startMinut>minuts){
                executeThread.interrupt();
                isFinished=true;
            }else {
                try {
                    executeThread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
        System.out.println("线程正常结束。。。");
    }
}

测试:

public class ThreadTest {
    public static void main(String[] args) {
        ExecuteThread execute=new ExecuteThread();
        long startMinut=System.currentTimeMillis();
        execute.exeThread(()->{
            while (true){
        //业务1
            }
        });
        execute.stopThread(3000); //三秒后结束任务
        long endMinut=System.currentTimeMillis();
        System.out.println("耗时"+(endMinut-startMinut));
    }
}
 

 

 

posted @ 2020-01-06 11:04  一人一见  阅读(101)  评论(0)    收藏  举报