Java多线程程序中synchronized修饰方法的使用实例
在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