首先来看同步方法的例子:
public class SynchronizedTest1 extends Thread
{
private synchronized void testSynchronizedMethod()
{
for (int i = 0; i < 10; i++)
{
System.out.println(Thread.currentThread().getName()
+ " testSynchronizedMethod:" + i);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
@Override
public void run()
{
testSynchronizedMethod();
}
public static void main(String[] args)
{
SynchronizedTest1 t = new SynchronizedTest1();
t.start();
t.testSynchronizedMethod();
}
}
运行该程序输出结果为:
main testSynchronizedMethod:0
main testSynchronizedMethod:1
main testSynchronizedMethod:2
main testSynchronizedMethod:3
main testSynchronizedMethod:4
main testSynchronizedMethod:5
main testSynchronizedMethod:6
main testSynchronizedMethod:7
main testSynchronizedMethod:8
main testSynchronizedMethod:9
Thread-0 testSynchronizedMethod:0
Thread-0 testSynchronizedMethod:1
Thread-0 testSynchronizedMethod:2
Thread-0 testSynchronizedMethod:3
Thread-0 testSynchronizedMethod:4
Thread-0 testSynchronizedMethod:5
Thread-0 testSynchronizedMethod:6
Thread-0 testSynchronizedMethod:7
Thread-0 testSynchronizedMethod:8
Thread-0 testSynchronizedMethod:9
可以看到testSynchronizedMethod方法在两个线程之间同步执行。
public static void main(String[] args)
{
Thread t = new SynchronizedTest1();
t.start();
Thread t1 = new SynchronizedTest1();
t1.start();
}
Thread-0 testSynchronizedMethod:0
Thread-1 testSynchronizedMethod:0
Thread-0 testSynchronizedMethod:1
Thread-1 testSynchronizedMethod:1
Thread-0 testSynchronizedMethod:2
Thread-1 testSynchronizedMethod:2
Thread-0 testSynchronizedMethod:3
Thread-1 testSynchronizedMethod:3
Thread-0 testSynchronizedMethod:4
Thread-1 testSynchronizedMethod:4
Thread-0 testSynchronizedMethod:5
Thread-1 testSynchronizedMethod:5
Thread-0 testSynchronizedMethod:6
Thread-1 testSynchronizedMethod:6
Thread-0 testSynchronizedMethod:7
Thread-1 testSynchronizedMethod:7
Thread-0 testSynchronizedMethod:8
Thread-1 testSynchronizedMethod:8
Thread-0 testSynchronizedMethod:9
Thread-1 testSynchronizedMethod:9
若想修改后的main方法能够在两个线程之间同步运行,需要将testSynchronizedMethod方法声明为静态方法,这样两个线程的监视器是同一个对象(类对象),能够同步执行。修改后的代码如下所示:
public class SynchronizedTest1 extends Thread
{
private static synchronized void testSynchronizedMethod()
{
for (int i = 0; i < 10; i++)
{
System.out.println(Thread.currentThread().getName()
+ " testSynchronizedMethod:" + i);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
@Override
public void run()
{
testSynchronizedMethod();
}
public static void main(String[] args)
{
Thread t = new SynchronizedTest1();
t.start();
Thread t1 = new SynchronizedTest1();
t1.start();
}
}
输出结果如下:
Thread-0 testSynchronizedMethod:0
Thread-0 testSynchronizedMethod:1
Thread-0 testSynchronizedMethod:2
Thread-0 testSynchronizedMethod:3
Thread-0 testSynchronizedMethod:4
Thread-0 testSynchronizedMethod:5
Thread-0 testSynchronizedMethod:6
Thread-0 testSynchronizedMethod:7
Thread-0 testSynchronizedMethod:8
Thread-0 testSynchronizedMethod:9
Thread-1 testSynchronizedMethod:0
Thread-1 testSynchronizedMethod:1
Thread-1 testSynchronizedMethod:2
Thread-1 testSynchronizedMethod:3
Thread-1 testSynchronizedMethod:4
Thread-1 testSynchronizedMethod:5
Thread-1 testSynchronizedMethod:6
Thread-1 testSynchronizedMethod:7
Thread-1 testSynchronizedMethod:8
Thread-1 testSynchronizedMethod:9
同步块的情况与同步方法类似,只是同步块将同步控制的粒度缩小,这样能够更好的发挥多线程并行执行的效率。
public class SynchronizedTest2 extends Thread
{
private void testSynchronizedBlock()
{
synchronized (this)
{
for (int i = 0; i < 10; i++)
{
System.out.println(Thread.currentThread().getName()
+ " testSynchronizedBlock:" + i);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
@Override
public void run()
{
testSynchronizedBlock();
}
public static void main(String[] args)
{
SynchronizedTest2 t = new SynchronizedTest2();
t.start();
t.testSynchronizedBlock();
}
}
输出结果:
main testSynchronizedBlock:0
main testSynchronizedBlock:1
main testSynchronizedBlock:2
main testSynchronizedBlock:3
main testSynchronizedBlock:4
main testSynchronizedBlock:5
main testSynchronizedBlock:6
main testSynchronizedBlock:7
main testSynchronizedBlock:8
main testSynchronizedBlock:9
Thread-0 testSynchronizedBlock:0
Thread-0 testSynchronizedBlock:1
Thread-0 testSynchronizedBlock:2
Thread-0 testSynchronizedBlock:3
Thread-0 testSynchronizedBlock:4
Thread-0 testSynchronizedBlock:5
Thread-0 testSynchronizedBlock:6
Thread-0 testSynchronizedBlock:7
Thread-0 testSynchronizedBlock:8
Thread-0 testSynchronizedBlock:9
使用class对象控制不同实例之间的同步:
public class SynchronizedTest2 extends Thread
{
private void testSynchronizedBlock()
{
synchronized (SynchronizedTest2.class)
{
for (int i = 0; i < 10; i++)
{
System.out.println(Thread.currentThread().getName()
+ " testSynchronizedBlock:" + i);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
@Override
public void run()
{
testSynchronizedBlock();
}
public static void main(String[] args)
{
Thread t = new SynchronizedTest2();
t.start();
Thread t2 = new SynchronizedTest2();
t2.start();
}
}
Thread-0 testSynchronizedBlock:0
Thread-0 testSynchronizedBlock:1
Thread-0 testSynchronizedBlock:2
Thread-0 testSynchronizedBlock:3
Thread-0 testSynchronizedBlock:4
Thread-0 testSynchronizedBlock:5
Thread-0 testSynchronizedBlock:6
Thread-0 testSynchronizedBlock:7
Thread-0 testSynchronizedBlock:8
Thread-0 testSynchronizedBlock:9
Thread-1 testSynchronizedBlock:0
Thread-1 testSynchronizedBlock:1
Thread-1 testSynchronizedBlock:2
Thread-1 testSynchronizedBlock:3
Thread-1 testSynchronizedBlock:4
Thread-1 testSynchronizedBlock:5
Thread-1 testSynchronizedBlock:6
Thread-1 testSynchronizedBlock:7
Thread-1 testSynchronizedBlock:8
Thread-1 testSynchronizedBlock:9
使用synchronized关键字进行同步控制时,一定要把握好对象监视器,只有获得监视器的进程可以运行,其它都需要等待获取监视器。任何一个非null的对象都可以作为对象监视器,当synchronized作用在方法上时,锁住的便是对象实例(this);当作用在静态方法时锁住的便是对象对应的Class实例。
总结:
synchronized是通过软件(JVM)实现的,简单易用,即使在JDK5之后有了Lock,仍然被广泛地使用。
synchronized实际上是非公平的,新来的线程有可能立即获得监视器,而在等待区中等候已久的线程可能再次等待,不过这种抢占的方式可以预防饥饿。
synchronized只有锁只与一个条件(是否获取锁)相关联,不灵活,后来Condition与Lock的结合解决了这个问题。
多线程竞争一个锁时,其余未得到锁的线程只能不停的尝试获得锁,而不能中断。高并发的情况下会导致性能下降。ReentrantLock的lockInterruptibly()方法可以优先考虑响应中断。 一个线程等待时间过长,它可以中断自己,然后ReentrantLock响应这个中断,不再让这个线程继续等待。有了这个机制,使用ReentrantLock时就不会像synchronized那样产生死锁了。
参考资料:
JAVA并发编程学习笔记之synchronized
深入JVM锁机制1-synchronized
java 对象锁