并发的常用方法

 1.线程上下文切换(Thread Context Switch)

1)线程的cpu时间片用完
2)垃圾回收会让工作的线程全部暂停,启动垃圾回收线程回收垃圾
3)有更高优先级的线程需要运行
4)线程自己调用sleep、yield、wait、join、park、synchronized、lock等方法
当Thread Context Switch发生时,操作系统保存当前线程的状态信息,并恢复另一个线程的状态
java中对应的是程序计数器(Program Counter Register)记录下一条jvm指令的执行地址。
Thread Context Switch切换频繁会影响性能

2.sleep

会让线程从Running状态转换到Timed_waiting阻塞状态,任务调度器不会将时间片分给这种线程
interruput()会打断sleep()并抛出InterruptedException异常
睡眠结束该线程以后未必立刻执行,等待cup分配时间片

public static void main(String[] args) {
        Thread t1=new Thread(()->{
            try {
                Thread.sleep(10000);
                System.out.println("t1被打断了");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t1.interrupt();
    }

 t1线程获得object锁,进入睡眠,会让出cpu时间片,但不会释放锁,t2线程获得时间片,但无法获得锁,进入EntryList中等待t1执行完毕释放锁。

如果t1睡眠时被打断,会执行catch中的语句,并释放锁。

private static Object object=new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            synchronized (object){
                try {
                    System.out.println("t1获得锁");
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t2=new Thread(()->{
           synchronized (object) {
               System.out.println("t2获得锁");
           }
        });

        t1.start();
        Thread.sleep(1);
        t2.start();
    }

 

3. yield: 

主动让出cpu时间片,会让线程从Running进入到Runnable就绪状态,然后让cpu去调度其他线程,
具体实现依赖于操作系统的任务调度器,任务调度器还有可能把时间片分给当前线程

Thread.yield()也只会让出cpu时间片,不会释放获得的锁 

 4.线程优先级

t1.setPriority():  1-10设置当前线程优先级,优先级越大,越有机会优先执行,但并不一定,和yield一样,需要依赖操作系统的任务调度器

5.join

t1线程睡眠10s,会让出时间片给主线程运行,但如果主线程想等待t1线程结束执行,就在主线程调用t1.join()

public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t1.join();
        System.out.println("主线程继续运行");
    }

 

6.interrupt

打断阻塞中的线程:t1线程睡眠10s进入阻塞状态,会让出时间片给主线程运行,主线程睡眠1ms后,调用t1.interrupt()打断t1线程的睡眠,t1线程会进入catch块抛出异常

public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        Thread.sleep(1);
        t1.interrupt();
    }

打断正常运行的线程:正常运行的线程被打断后,打断标记Thread.currentThread().isInterrupted()会变为true,但当前线程会继续运行,不会停止

可以通过对打断标记Thread.currentThread().isInterrupted()的判断来结束当前线程的运行

public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            while(true){
                if(Thread.currentThread().isInterrupted()){
                    break;
                }
                System.out.println("hello");
            }
        });
        t1.start();
        Thread.sleep(1);
        t1.interrupt();
    }

 

7.park

park()是java.util.concurrent.locks.LockSupport 中的一个方法,调用该方法可以使当前线程进入阻塞状态,如果没有其他线程打断,会一直阻塞下去

如下图代码中,打印完hello后,t1线程会进入阻塞,如果没有其他线程打断,则会一直阻塞下去,不会打印hello02

public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            System.out.println("hello");
            LockSupport.park();
            System.out.println("hello02");
        });

        t1.start();
        Thread.sleep(1000);
        //t1.interrupt();
    }

如果有其他线程打断t1线程,会从park()方法下面继续执行t1线程的代码,不会像sleep一样,被打断后就抛出异常。

如下图代码,t1线程被打断后,会继续打印hello02

    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            System.out.println("hello");
            LockSupport.park();
            System.out.println("hello02");
        });

        t1.start();
        Thread.sleep(1000);
        t1.interrupt();
    }

如果打断标记Thread.currentThread().isInterrupted()为true,park()方法会失效

如下图代码:

第一次t1线程打印完hello后,调用park()打断,然后主线程打断t1线程,t1线程会继续运行,打印hello02,但再次调用park()时,没有作用,t1线程并没有停止运行,而是继续打印了hello03

public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            System.out.println("hello");
            LockSupport.park();
            System.out.println("hello02");
            LockSupport.park();
            System.out.println("hello03");
        });
        t1.start();
        Thread.sleep(1);
        t1.interrupt();
    }

为了解决上述问题,可以调用Thread.interrupted()方法,调用该方法后,会把打断标记设为false,park()又可以起作用了

public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            System.out.println("hello");
            LockSupport.park();
            System.out.println("hello02");
            Thread.interrupted();
            LockSupport.park();
            System.out.println("hello03");
        });
        t1.start();
        Thread.sleep(1);
        t1.interrupt();
    }

8.unpark

LockSupport.unpark()可以打断LockSupport.park(),可以在park()之前调用,也可以在park()之后调用

在park()之后调用unpark()就等于阻塞后唤醒,在park()之前调用unpark()则不会阻塞,会持续运行

下图是原理部分

 

 

9.守护线程

如果线程被设置为守护线程,即使守护线程并没有执行完毕,但其他非守护线程已经执行完毕,守护线程也会停止运行

如下图代码:

t1线程是死循环,不会停止运行,但把t1线程设置为守护线程,只要t2线程执行结束,t1线程也会立刻结束

public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            while (true){}
        });

        Thread t2=new Thread(()->{
            System.out.println("hello");
        });

        t1.setDaemon(true);
        t1.start();
        t2.start();
    }

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值