Java多线程程序中synchronized修饰方法的使用实例
作者:fireshort
在Java 5以前,是用synchronized关键字来实现锁的功能。
synchronized关键字可以作为方法的修饰符(同步方法),也可作用于函数内的语句(同步代码块)。
掌握synchronized,关键是要掌握把那个东西作为锁。对于类的非静态方法(成员方法)而言,意味着要取得对象实例的锁;对于类的静态方法(类方法)而言,要取得类的Class对象的锁;对于同步代码块,要指定取得的是哪个对象的锁。同步非静态方法可以视为包含整个方法的synchronized(this) { … }代码块。
不管是同步代码块还是同步方法,每次只有一个线程可以进入(在同一时刻最多只有一个线程执行该段代码。),如果其他线程试图进入(不管是同一同步块还是不同的同步块),jvm会将它们挂起(放入到等锁池中)。这种结构在并发理论中称为临界区(critical section)。
在jvm内部,为了提高效率,同时运行的每个线程都会有它正在处理的数据的缓存副本,当我们使用synchronzied进行同步的时候,真正被同步的是在不同线程中表示被锁定对象的内存块(副本数据会保持和主内存的同步,现在知道为什么要用同步这个词汇了吧),简单的说就是在同步块或同步方法执行完后,对被锁定的对象做的任何修改要在释放锁之前写回到主内存中;在进入同步块得到锁之后,被锁定对象的数据是从主内存中读出来的,持有锁的线程的数据副本一定和主内存中的数据视图是同步的 。
下面举具体的例子来说明synchronized的各种情况。
synchronized同步方法
首先来看同步方法的例子:
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实例
两个线程同时访问一个对象的同步方法
当两个并发线程访问同一个对象的同步方法时,只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个以后才能执行。
public class TwoThread { public static void main(String[] args) { final TwoThread twoThread = new TwoThread(); Thread t1 = new Thread(new Runnable() { public void run() { twoThread.syncMethod(); } }, "A"); Thread t2 = new Thread(new Runnable() { public void run() { twoThread.syncMethod(); } }, "B"); t1.start(); t2.start(); } public synchronized void syncMethod() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " : " + i); try { Thread.sleep(500); } catch (InterruptedException ie) { } } } }
输出结果:
A : 0 A : 1 A : 2 A : 3 A : 4 B : 0 B : 1 B : 2 B : 3 B : 4
两个线程访问的是两个对象的同步方法
这种情况下,synchronized不起作用,跟普通的方法一样。因为对应的锁是各自的对象。
public class TwoObject { public static void main(String[] args) { final TwoObject object1 = new TwoObject(); Thread t1 = new Thread(new Runnable() { public void run() { object1.syncMethod(); } }, "Object1"); t1.start(); final TwoObject object2 = new TwoObject(); Thread t2 = new Thread(new Runnable() { public void run() { object2.syncMethod(); } }, "Object2"); t2.start(); } public synchronized void syncMethod() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " : " + i); try { Thread.sleep(500); } catch (InterruptedException ie) { } } } }
其中一种可能的输出结果:
Object2 : 0 Object1 : 0 Object1 : 1 Object2 : 1 Object2 : 2 Object1 : 2 Object2 : 3 Object1 : 3 Object1 : 4 Object2 : 4
两个线程访问的是synchronized的静态方法
这种情况,由于锁住的是Class,在任何时候,该静态方法只有一个线程可以执行。
同时访问同步方法与非同步方法
当一个线程访问对象的一个同步方法时,另一个线程仍然可以访问该对象中的非同步方法。
public class SyncAndNoSync { public static void main(String[] args) { final SyncAndNoSync syncAndNoSync = new SyncAndNoSync(); Thread t1 = new Thread(new Runnable() { public void run() { syncAndNoSync.syncMethod(); } }, "A"); t1.start(); Thread t2 = new Thread(new Runnable() { public void run() { syncAndNoSync.noSyncMethod(); } }, "B"); t2.start(); } public synchronized void syncMethod() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " at syncMethod(): " + i); try { Thread.sleep(500); } catch (InterruptedException ie) { } } } public void noSyncMethod() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " at noSyncMethod(): " + i); try { Thread.sleep(500); } catch (InterruptedException ie) { } } } }
一种可能的输出结果:
B at noSyncMethod(): 0 A at syncMethod(): 0 B at noSyncMethod(): 1 A at syncMethod(): 1 B at noSyncMethod(): 2 A at syncMethod(): 2 B at noSyncMethod(): 3 A at syncMethod(): 3 A at syncMethod(): 4 B at noSyncMethod(): 4
访问同一个对象的不同同步方法
当一个线程访问一个对象的同步方法A时,其他线程对该对象中所有其它同步方法的访问将被阻塞。因为第一个线程已经获得了对象锁,其他线程得不到锁,则虽然是访问不同的方法,但是没有获得锁,也无法访问。
public class TwoSyncMethod { public static void main(String[] args) { final TwoSyncMethod twoSyncMethod = new TwoSyncMethod(); Thread t1 = new Thread(new Runnable() { public void run() { twoSyncMethod.syncMethod1(); } }, "A"); t1.start(); Thread t2 = new Thread(new Runnable() { public void run() { twoSyncMethod.syncMethod2(); } }, "B"); t2.start(); } public synchronized void syncMethod1() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " at syncMethod1(): " + i); try { Thread.sleep(500); } catch (InterruptedException ie) { } } } public synchronized void syncMethod2() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " at syncMethod2(): " + i); try { Thread.sleep(500); } catch (InterruptedException ie) { } } } }
输出结果:
A at syncMethod1(): 0 A at syncMethod1(): 1 A at syncMethod1(): 2 A at syncMethod1(): 3 A at syncMethod1(): 4 B at syncMethod2(): 0 B at syncMethod2(): 1 B at syncMethod2(): 2 B at syncMethod2(): 3 B at syncMethod2(): 4
您可能感兴趣的文章:
- Java线程安全解决方案(synchronized,ReentrantLock,Atomic)
- Java synchronized关键字和Lock接口实现原理
- 深入理解java内置锁(synchronized)和显式锁(ReentrantLock)
- Java编程synchronized与lock的区别【推荐】
- 深入Synchronized和java.util.concurrent.locks.Lock的区别详解
- Java多线程实现TCP网络Socket编程(C/S通信)
- Java创建多线程的几种方式实现
- java多线程之Future和FutureTask使用实例
- Java多线程synchronized同步方法详解
- java多线程之线程,进程和Synchronized概念初解
- Java 多线程Synchronized和Lock的区别