并发机制原理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