public class AccountingSync implements Runnable{
//共享资源(临界资源)
static int i=0;
/** * synchronized 修饰实例方法 * 暂时先不添加 */
public void increase(){
i++;
}
@Override
public void run() {
for(int j=0;j<1000000;j++){
increase();
}
}
public static void main(String[] args) throws InterruptedException {
AccountingSync instance=new AccountingSync();
Thread t1=new Thread(instance);
Thread t2=new Thread(instance);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(i);
}
/** * 输出结果: * 1802452 */
}
复制代码
i++ 赋值操做并无 原子性 在读取原来的参数和返回新的参数的这段时间中,若是咱们第二个线程也作了一样的操做,两个线程看到的参数是同样的,一样执行了 +1 的操做,这里就形成了线程安全破坏,咱们最终输出的结果是 1802452java
若是咱们添加上 synchronized 此时 increase() 方法在一个时间内 只能被一个线程读写,也就避免脏数据的产生。编程
可是这样也不是安全的,若是咱们 new 出两个 AccountingSync 对象去执行操做 结果是怎么样?安全
public class AccountingSyncBad implements Runnable{
static int i=0;
public synchronized void increase(){
i++;
}
@Override
public void run() {
for(int j=0;j<1000000;j++){
increase();
}
}
public static void main(String[] args) throws InterruptedException {
//new新实例
Thread t1=new Thread(new AccountingSyncBad());
//new新实例
Thread t2=new Thread(new AccountingSyncBad());
t1.start();
t2.start();
//join含义:当前线程A等待thread线程终止以后才能从thread.join()返回
t1.join();
t2.join();
System.out.println(i);
}
}
复制代码
结果依旧是产生了脏数据,缘由是两个实例对象锁并不一样相同,此时若是两个线程操做数据并不是共享的,线程安全是有保障的,遗憾的是若是两个线程操做的是共享数据,安全将没有保障,他们自己的锁只能保持自己数据结构
public class AccountingSyncClass implements Runnable{
static int i=0;
/** * 做用于静态方法,锁是当前class对象,也就是 * AccountingSyncClass类对应的class对象 */
public static synchronized void increase(){
i++;
}
/** * 非静态,访问时锁不同不会发生互斥 */
public synchronized void increase4Obj(){
i++;
}
@Override
public void run() {
for(int j=0;j<1000000;j++){
increase();
}
}
public static void main(String[] args) throws InterruptedException {
//new新实例
Thread t1=new Thread(new AccountingSyncClass());
//new心事了
Thread t2=new Thread(new AccountingSyncClass());
//启动线程
t1.start();t2.start();
t1.join();t2.join();
System.out.println(i);
}
}
复制代码
这个实例当中咱们将 synchronized 做用于静态方法了,由于静态方法不属于任何实例对象,它是类成员,因此这把锁也能够理解为加在了 Class 上 ,可是若是咱们线程A 调用了 class 内部 static synchronized 方法 线程B 调用了 class 内部 非 static 方法 是能够的,不会发生互斥现象,由于访问静态 synchronized 方法占用的锁是当前类的class对象,而访问非静态 synchronized 方法占用的锁是当前实例对象锁多线程
public class AccountingSync implements Runnable{
static AccountingSync instance=new AccountingSync();
static int i=0;
@Override
public void run() {
//省略其余耗时操做....
//使用同步代码块对变量i进行同步操做,锁对象为instance
synchronized(instance){
for(int j=0;j<1000000;j++){
i++;
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread t1=new Thread(instance);
Thread t2=new Thread(instance);
t1.start();t2.start();
t1.join();t2.join();
System.out.println(i);
}
}
复制代码
内部使用 monitorenter 和 monitorexit 指令实现,monitorenter 指令插入到同步代码块的开始位置,monitorexit 指令插入到同步代码块的结束位置,jvm须要保证每个monitorenter都有一个 monitorexit 与之对应。任何一个对象都有一个 monitor 与之相关联,当它的monitor被持有以后,它将处于锁定状态。线程执行到 monitorenter 指令前,将会尝试获取对象所对应的 monitor 全部权,即尝试获取对象的锁;将线程执行到 monitorexit 时就会释放锁。jvm
(人话:)每一个对象都会与一个monitor相关联,当某个monitor被拥有以后就会被锁住,当线程执行到monitorenter指令时,就会去尝试得到对应的monitor。步骤以下:ide
每一个monitor维护着一个记录着拥有次数的计数器。未被拥有的monitor的计数器为0,当一个线程得到monitor(执行monitorenter)后,该计数器自增变为 1 。当同一个线程再次得到该monitor的时候,计数器再次自增;当不一样线程想要得到该monitor的时候,就会被阻塞。post
当同一个线程释放monitor(执行monitorexit指令)的时候,计数器再自减。当计数器为0的时候。monitor将被释放,其余线程即可以得到monitor。 spa
不过相对于普通方法,其常量池中多了ACC_SYNCHRONIZED标示符。JVM就是根据该标示符来实现方法的同步的:当方法调用时,调用指令将会检查方法的 ACC_SYNCHRONIZED 访问标志是否被设置,若是设置了,执行线程将先获取monitor,获取成功以后才能执行方法体,方法执行完后再释放monitor。线程
在jvm字节码层面并无任何特别的指令来实现synchronized修饰的方法,而是在class文件中将该方法的access_flags字段中的acc_synchronized标志位设置为1,表示该方法为synchronized方法。
在java设计中,每个对象自打娘胎里出来就带了一把看不见的锁,即monitor锁。monitor是线程私有的数据结构,每个线程都有一个monitor record列表,同时还有一个全局可用列表。每个被锁住对象都会和一个monitor关联。monitor中有一个owner字段存放拥有该对象的线程的惟一标识,表示该锁被这个线程占有。owner:初始时为null,表示当前没有任何线程拥有该monitor,当线程成功拥有该锁后,owner保存线程惟一标识,当锁被释放时,owner又变为null。