JAVA多线程join()方法细解 到底释放锁吗?

Join方法细解 到底释放锁嘛??!

先看:推荐阅读 食用更佳

join方法能干什么?在A线程中调用B.join()那么B线程会获得cpu执行。 当前线程进入WAITING/TIMED_WAITING状态,线程t执行完毕或者millis时间到,当前线程t一般情况下进入RUNNABLE状态

源码:

public final synchronized void join(long millis)
    throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;

        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (millis == 0) {
            while (isAlive()) {
                wait(0);  
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }

底层调用了 wait()

wait方法:native方法 作用是释放调用对象的对象锁,当前线程进入等待状态。()

t1.wait(); 会释放t1 这个线程的对象锁 也就是说t1.join()会释放t1这个对象的锁,其他的锁不会释放

看例子:

package test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @className: t1
 * @description: TODO 类描述
 * @author: Dong
 * @date: 2023/2/28
 **/
public class t1 {


        public static void main(String[] args) {
            Object object = new MThread();
            MThread mythread = new MThread("mythread ", object);
            mythread.start();
            //synchronized (mythread)
            synchronized (mythread) {
                System.out.println("mythread = " + mythread.hashCode());
                for (int i = 0; i < 100; i++) {
                    if (i == 20) {
                        try {
                            System.out.println("开始join");
                            mythread.join();//main主线程让出CPU执行权,让mythread子线程优先执行
                            System.out.println("结束join");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName() +"==" + i);
                }
            }
            System.out.println("main方法执行完毕");
        }
    }

    class MThread extends Thread {
        private String name;
        private Object object;
        public MThread() {
        }
        public MThread(String name, Object obj) {
            this.name = name;
            this.object = obj;
        }
        @Override
        public void run() {
            synchronized (this) { //this
                System.out.println("this = " + this.hashCode());
                for (int i = 0; i < 100; i++) {
                    System.out.println(name + i);
                }
            }
        }


}

会释放,顺利执行

package test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @className: t1
 * @description: TODO 类描述
 * @author: Dong
 * @date: 2023/2/28
 **/
public class t1 {


        public static void main(String[] args) {
            Object object = new MThread();
            MThread mythread = new MThread("mythread ", object);
            mythread.start();
            //synchronized (mythread)
            synchronized (object) {
                System.out.println("mythread = " + mythread.hashCode());
                for (int i = 0; i < 100; i++) {
                    if (i == 20) {
                        try {
                            System.out.println("开始join");
                            mythread.join();//main主线程让出CPU执行权,让mythread子线程优先执行
                            System.out.println("结束join");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName() +"==" + i);
                }
            }
            System.out.println("main方法执行完毕");
        }
    }

    class MThread extends Thread {
        private String name;
        private Object object;
        public MThread() {
        }
        public MThread(String name, Object obj) {
            this.name = name;
            this.object = obj;
        }
        @Override
        public void run() {
            synchronized (object) { //this
                System.out.println("this = " + this.hashCode());
                for (int i = 0; i < 100; i++) {
                    System.out.println(name + i);
                }
            }
        }


}

不会释放 因为join的调用对象是mythread,释放的是mythreda的锁。

总结: join释放锁 只释放调用对象的对象锁

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值