原文链接:https://www.iteye.com/blog/zhh9106-2151791
先回顾一下synchronized的用法:
1.同步代码块(对象锁)
public class SynchronizedObjectLock implements Runnable {
static SynchronizedObjectLock instence = new SynchronizedObjectLock();
@Override
public void run() {
// 同步代码块形式——锁为this,两个线程使用的锁是一样的,线程1必须要等到线程0释放了该锁后,才能执行
synchronized (this) {
System.out.println("我是线程" + Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "结束");
}
}
public static void main(String[] args) {
Thread t1 = new Thread(instence,"Thread-0");
Thread t2 = new Thread(instence,"Thread-1");
t1.start();
t2.start();
}
}
我是线程Thread-0
Thread-0结束
我是线程Thread-1
Thread-1结束
代码块中的synchronized (this),指获取类SynchronizedObjectLock创建的实例对象instence中的内置锁(锁定当前代码块执行者),可以看到线程1必须要等到线程0释放了该锁后,才能执行。
2.同步方法(对象锁)
public class SynchronizedObjectLock3 implements Runnable {
static SynchronizedObjectLock3 instence3 = new SynchronizedObjectLock3();
@Override
public void run() {
method();
}
public synchronized void method() {
System.out.println("我是线程" + Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "结束");
}
public static void main(String[] args) {
Thread t1 = new Thread(instence3,"Thread-0");
Thread t2 = new Thread(instence3,"Thread-1");
t1.start();
t2.start();
}
}
我是线程Thread-0
Thread-0结束
我是线程Thread-1
Thread-1结束
synchronized 指锁定类SynchronizedObjectLock3创建的实例对象instence3的内置锁(锁定当前方法调用者),可以看到线程1必须要等到线程0释放了该锁后,才能执行。
public class SynchronizedObjectLock4 implements Runnable {
static SynchronizedObjectLock4 instence2 = new SynchronizedObjectLock4();
static SynchronizedObjectLock4 instence4 = new SynchronizedObjectLock4();
@Override
public void run() {
method();
}
// synchronized用在普通方法上,默认的锁就是this,当前实例
public synchronized void method() {
System.out.println("我是线程" + Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "结束");
}
public static void main(String[] args) {
// t1和t2对应的this是两个不同的实例,所以代码不会串行
Thread t1 = new Thread(instence2,"Thread-0");
Thread t2 = new Thread(instence4,"Thread-1");
t1.start();
t2.start();
}
}
我是线程Thread-1
我是线程Thread-0
Thread-0结束
Thread-1结束
或
我是线程Thread-0
我是线程Thread-1
Thread-1结束
Thread-0结束
注:还会有别的结果。
可以看到线程0和1的执行顺序不能保证,因为这两个线程获取的是SynchronizedObjectLock4类的不同实例的不同内置锁。
3.静态方法(类锁)
public class SynchronizedObjectLock5 implements Runnable {
static SynchronizedObjectLock5 instence1 = new SynchronizedObjectLock5();
static SynchronizedObjectLock5 instence2 = new SynchronizedObjectLock5();
@Override
public void run() {
method();
}
// synchronized用在静态方法上,默认的锁就是当前所在的Class类,所以无论是哪个线程访问它,需要的锁都只有一把
public static synchronized void method() { //不能保证线程1,2抢到锁的顺序,也
System.out.println("我是线程" + Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "结束");
}
public static void main(String[] args) {
Thread t1 = new Thread(instence1,"Thread-0");
Thread t2 = new Thread(instence2,"Thread-1");
t1.start();
t2.start();
}
}
我是线程Thread-0
Thread-0结束
我是线程Thread-1
Thread-1结束
此例,虽然类SynchronizedObjectLock5有两个不同的实例,但结果总是线程0先执行,然后1再执行。这是因为静态方法获取的是SynchronizedObjectLock5的类锁,和实例无关。且类锁只有一个,所以实现了同步
4.同步代码块(类锁)
public class SynchronizedObjectLock6 implements Runnable {
static SynchronizedObjectLock6 instence1 = new SynchronizedObjectLock6();
static SynchronizedObjectLock6 instence2 = new SynchronizedObjectLock6();
@Override
public void run() {
// 所有线程需要的锁都是同一把
synchronized(SynchronizedObjectLock6.class){
System.out.println("我是线程" + Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "结束");
}
}
public static void main(String[] args) {
Thread t1 = new Thread(instence1,"Thread-0");
Thread t2 = new Thread(instence2,"Thread-1");
t1.start();
t2.start();
}
}
我是线程Thread-0
Thread-0结束
我是线程Thread-1
Thread-1结束
获取类SynchronizedObjectLock6的类锁。
对象锁和类锁
java的内置锁:每个java对象都可以用做一个实现同步的锁,这些锁成为内置锁。线程进入同步代码块或方法的时候会自动获得该锁,在退出同步代码块或方法时会释放该锁。获得内置锁的唯一途径就是进入这个锁的保护的同步代码块或方法。
java内置锁是一个互斥锁,这就是意味着最多只有一个线程能够获得该锁,当线程A尝试去获得线程B持有的内置锁时,线程A必须等待或者阻塞,知道线程B释放这个锁,如果B线程不释放这个锁,那么A线程将永远等待下去。
java的对象锁和类锁:java的对象锁和类锁在锁的概念上基本上和内置锁是一致的,但是,两个锁实际是有很大的区别的,对象锁是用于对象实例方法,或者一个对象实例上的,类锁是用于类的静态方法或者一个类的class对象上的。我们知道,类的对象实例可以有很多个,但是每个类只有一个class对象,所以不同对象实例的对象锁是互不干扰的,但是每个类只有一个类锁。但是有一点必须注意的是,其实类锁只是一个概念上的东西,并不是真实存在的,它只是用来帮助我们理解锁定实例方法和静态方法的区别的。
对象锁—synchronized修饰方法和代码块:
public class TestSynchronized
{
public void test1()
{
synchronized(this)
{
int i = 5;
while( i-- > 0)
{
System.out.println(Thread.currentThread().getName() + " : " + i);
try
{
Thread.sleep(500);
}
catch (InterruptedException ie)
{
}
}
}
}
public synchronized void test2()
{
int i = 5;
while( i-- > 0)
{
System.out.println(Thread.currentThread().getName() + " : " + i);
try
{
Thread.sleep(500);
}
catch (InterruptedException ie)
{
}
}
}
public static void main(String[] args)
{
final TestSynchronized myt2 = new TestSynchronized();
Thread test1 = new Thread( new Runnable() { public void run() { myt2.test1(); } }, "test1" );
Thread test2 = new Thread( new Runnable() { public void run() { myt2.test2(); } }, "test2" );
test1.start();;
test2.start();
}
}
test1 : 4
test1 : 3
test1 : 2
test1 : 1
test1 : 0
test2 : 4
test2 : 3
test2 : 2
test2 : 1
test2 : 0
上述的代码,第一个方法时用了同步代码块的方式进行同步,传入的对象实例是this,表明是当前对象;第二个方法是修饰方法的方式进行同步。因为第一个同步代码块传入的this,所以两个同步代码所需要获得的对象锁都是同一个TestSynchronized对象的锁,下面main方法时分别开启两个线程,分别调用test1和test2方法,那么两个线程都需要获得该对象锁,另一个线程必须等待。上面也给出了运行的结果可以看到:直到test1线程执行完毕,释放掉锁,test2线程才开始执行。(原文链接中说是由于指令重排序,test2会先执行,但我多次运行没用出现这种情况,怀疑和JDK版本有关(本人用JDK1.8))
把test2方法的synchronized关键字去掉,执行结果会如何呢?
test1 : 4
test2 : 4
test1 : 3
test2 : 3
test1 : 2
test2 : 2
test2 : 1
test1 : 1
test2 : 0
test1 : 0
上面是执行结果,可以看到,结果输出是交替着进行输出的,这是因为,某个线程得到了对象锁,但是另一个线程还是可以访问没有进行同步的方法或者代码。进行了同步的方法(加锁方法)和没有进行同步的方法(普通方法)是互不影响的,一个线程进入了同步方法,得到了对象锁,其他线程还是可以访问那些没有同步的方法(普通方法)。原因在于:对象的内置锁和对象的状态之间是没有内在的关联的。当获取到与对象关联的内置锁时,并不能阻止其他线程访问该对象,当某个线程获得对象的锁之后,只能阻止其他线程获得同一个锁。
总结:synchronized只是一个内置锁的加锁机制,当某个方法加上synchronized关键字后,就表明要获得该内置锁才能执行,但并不能阻止其他线程访问不需要获得该内置锁的方法。
类锁—修饰静态方法和代码块:
public class TestSynchronized1
{
public void test1()
{
synchronized(TestSynchronized1.class)
{
int i = 5;
while( i-- > 0)
{
System.out.println(Thread.currentThread().getName() + " : " + i);
try
{
Thread.sleep(500);
}
catch (InterruptedException ie)
{
}
}
}
}
public static synchronized void test2()
{
int i = 5;
while( i-- > 0)
{
System.out.println(Thread.currentThread().getName() + " : " + i);
try
{
Thread.sleep(500);
}
catch (InterruptedException ie)
{
}
}
}
public static void main(String[] args)
{
final TestSynchronized1 myt2 = new TestSynchronized1();
Thread test1 = new Thread( new Runnable() { public void run() { myt2.test1(); } }, "test1" );
Thread test2 = new Thread( new Runnable() { public void run() { TestSynchronized1.test2(); } }, "test2" );
test1.start();
test2.start();
}
}
test1 : 4
test1 : 3
test1 : 2
test1 : 1
test1 : 0
test2 : 4
test2 : 3
test2 : 2
test2 : 1
test2 : 0
可以看到线程2要在线程1执行完之后,才开始执行,这是因为两个synchronized加的都是类锁,而且是同一个类的锁,一个类只有一把锁,所以加的是同一个类的同一把锁,所以要等待。
类锁-对象锁
public class TestSynchronized2
{
public synchronized void test1()
{
int i = 5;
while( i-- > 0)
{
System.out.println(Thread.currentThread().getName() + " : " + i);
try
{
Thread.sleep(500);
}
catch (InterruptedException ie)
{
}
}
}
public static synchronized void test2()
{
int i = 5;
while( i-- > 0)
{
System.out.println(Thread.currentThread().getName() + " : " + i);
try
{
Thread.sleep(500);
}
catch (InterruptedException ie)
{
}
}
}
public static void main(String[] args)
{
final TestSynchronized2 myt2 = new TestSynchronized2();
Thread test1 = new Thread( new Runnable() { public void run() { myt2.test1(); } }, "test1" );
Thread test2 = new Thread( new Runnable() { public void run() { TestSynchronized2.test2(); } }, "test2" );
test1.start();
test2.start();
}
}
test1 : 4
test2 : 4
test2 : 3
test1 : 3
test1 : 2
test2 : 2
test1 : 1
test2 : 1
test2 : 0
test1 : 0
上面代码synchronized同时修饰静态方法和实例方法,但是运行结果是交替进行的,这证明了类锁和对象锁是两个不一样的锁,控制着不同的区域,它们是互不干扰的。同样,线程获得对象锁的同时,也可以获得该类锁,即同时获得两个锁,这是允许的。
疑问:既然有了synchronized修饰方法的同步方式,为什么还需要synchronized修饰同步代码块的方式呢?
synchronized的缺陷:当某个线程进入同步方法获得对象锁,那么其他线程访问这里对象的同步方法时,必须等待或者阻塞,如果这个方法代码比较多,那等待线程的等待时间就会比较长,直到当前执行方法的线程执行完方法,释放锁,等待线程才可以获得锁去执行。如果使用同步代码块,它可以更加灵活的选择要同步的代码,而不用整个方法都加锁,这样可以提高代码执行效率,类似于Lock的lock()方法和unLock()方法优点。
Java Synchronized详解:对象锁与类锁的应用与区别
本文详细介绍了Java中的synchronized关键字的四种使用方式:同步代码块(对象锁)、同步方法(对象锁)、静态同步方法(类锁)和同步代码块(类锁)。通过示例代码展示了它们的执行效果,强调了对象锁和类锁的区别,以及同步代码块在提高代码效率方面的优势。同时,讨论了synchronized修饰方法和同步代码块的必要性,并指出类锁与对象锁互不影响的特性。
1053

被折叠的 条评论
为什么被折叠?



