Java同步之synchronized

Java中可以使用关键字synchronized进行线程同步控制,实现关键资源顺序访问,避免由于多线程并发执行导致的数据不一致性等问题。synchronized的原理是对象监视器(锁),只有获取到监视器的线程才能继续执行,否则线程会等待获取监视器。Java中每个对象或者类都有一把锁与之相关联,对于对象来说,监视的是这个对象的实例变量,对于类来说,监视的是类变量(一个类本身是类Class的对象,所以与类关联的锁也是对象锁)。synchronized关键字使用方式有两种:synchronized方法和synchronized块。这两种监视区域都和一个引入对象相关联,当到达这个监视区域时,JVM就会锁住这个引用对象,当离开时会释放这个引用对象上的锁(有异常退出时,JVM会释放锁)。对象锁是JVM内部机制,只需要编写同步方法或者同步块即可,操作监视区域时JVM会自动获取锁或者释放锁。

首先来看同步方法的例子:

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方法在两个线程之间同步执行。

如果此时将main方法修改为如下所示,则两个线程并不能同步执行,因为此时两个线程的同步监视器不是同一个对象,不能起到同步的作用。

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
同步块的情况与同步方法类似,只是同步块将同步控制的粒度缩小,这样能够更好的发挥多线程并行执行的效率。
使用this对象控制同一对象实例之间的同步:

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 对象锁

你可能感兴趣的:(Java,进程与线程)