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释放锁 只释放调用对象的对象锁

你可能感兴趣的:(问题总结,java基础,java,jvm,开发语言)