sychronized重量级锁之管程模型

并发机制原理2-sychronized重量级锁之管程模型

JAVA线程与内核线程的关系

在这里插入图片描述

public class Thread implements Runnable {
   
    /* Make sure registerNatives is the first thing <clinit> does. */
    private static native void registerNatives();
    static {
   
        registerNatives();
    }

这个是Thread的源码的类信息,首先实现了Runnable接口,这个接口中run方法是我们线程执行的具体业务逻辑方法,如果创建了线程,则要实现这个run方法,实现我们自己的业务逻辑,线程调用是要交给内核去处理的,而java和jvm都是用户态的模式,所有需要调用本地c++方法开启内核线程,所以registerNatives就是注册本地方法

public synchronized void start() {
   
    /**
     * This method is not invoked for the main method thread or "system"
     * group threads created/set up by the VM. Any new functionality added
     * to this method in the future may have to also be added to the VM.
     *
     * A zero status value corresponds to state "NEW".
     */
    if (threadStatus != 0)
        throw new IllegalThreadStateException();

    /* Notify the group that this thread is about to be started
     * so that it can be added to the group's list of threads
     * and the group's unstarted count can be decremented. */
    group.add(this);

    boolean started = false;
    try {
   
        start0();
        started = true;
    } finally {
   
        try {
   
            if (!started) {
   
                group.threadStartFailed(this);
            }
        } catch (Throwable ignore) {
   
            /* do nothing. If start0 threw a Throwable then
              it will be passed up the call stack */
        }
    }
}
private native void start0();

线程之间的通信

synchronized 使用方式

//修饰普通方法
public synchronized void increase(){
   
    i++;
}

//修饰静态方法
public static synchronized
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值