在日常的开发中咱们处理对象及变量的并发访问(保证数据的线程性安全)时都会想到synchronized
关键字。固然碰到些具体实例还有其余选择用于保证多线程访问资源的同步性,例如:ReentrantLock
、ReentrantReadWriteLock
。安全
咱们首先来了解下什么对象锁,什么是类锁?对象锁是用于对象实例方法,或者一个对象实例上的,类锁是用于类的静态方法或者一个类的class对象上的bash
sychronized
关键字添加到static
静态方法上或synchonized(Class)
代码块是给Class类上锁sychronized
关键字添加到非static
静态方法上是给对象上锁从以上两点的定义上可能理解起来有些吃力,下边咱们经过几个demon来验证下。多线程
public class Service {
synchronized public static void methodA() {
try {
System.out.println("methodA start by " + Thread.currentThread().getName()+" at time " + System.currentTimeMillis());
Thread.sleep(3000);
System.out.println("methodA end by " + Thread.currentThread().getName()+" at time " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized public static void methodB() {
try {
System.out.println("methodB start by " + Thread.currentThread().getName() +" at time " + System.currentTimeMillis());
Thread.sleep(3000);
System.out.println("methodB end by " + Thread.currentThread().getName()+" at time " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized public void methodC() {
try {
System.out.println("methodC start by " + Thread.currentThread().getName()+" at time " + System.currentTimeMillis());
Thread.sleep(3000);
System.out.println("methodC end by " + Thread.currentThread().getName()+" at time " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
复制代码
Test实例 并发
咱们来分析下运行结果:methodA
methodC
两线程交叉(异步)执行,故此咱们有这样的一个结论这两个线程分别抢占的是不一样的锁一个是类锁(methodA),一个是对象锁(methodC)。更深层次的说明了类锁和对象锁是两个不同的锁,控制着不一样的区域,它们是互不干扰的。一样,线程得到对象锁的同时,也能够得到该类锁,即同时得到两个锁,这是容许的.异步
methodA
与methodB
都是抢占同一个类锁,这两线程的运行结果符合预期的同步执行。高并发
三人行,必有我师。最近都进行多线程以及高并发这块的扫盲,在给你们分享干货的同时,才疏学浅还望你们大刀予以斧正。也欢迎关注个人掘金或简书,名称为柴码
spa