首先这个问题涉及到的是Java的对象锁。
java的对象锁和类锁:java的对象锁和类锁在锁的概念上基本上和内置锁是一致的,但是,两个锁实际是有很大的区别的,对象锁是用于对象实例方法,或者一个对象实例上的,类锁是用于类的静态方法或者一个类的class对象上的。我们知道,类的对象实例可以有很多个,但是每个类只有一个class对象,所以不同对象实例的对象锁是互不干扰的,但是每个类只有一个类锁。但是有一点必须注意的是,其实类锁只是一个概念上的东西,并不是真实存在的,它只是用来帮助我们理解锁定实例方法和静态方法的区别的。
- 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();
- // TestRunnable tr=new TestRunnable();
- // Thread test3=new Thread(tr);
- // test3.start();
- }
- }
- test2 : 4
- test2 : 3
- test2 : 2
- test2 : 1
- test2 : 0
- test1 : 4
- test1 : 3
- test1 : 2
- test1 : 1
- test1 : 0
如果我们把test2方法的synchronized关键字去掉,执行结果会如何呢?
运行结果:
- test1 : 4
- test2 : 4
- test2 : 3
- test1 : 3
- test1 : 2
- test2 : 2
- test2 : 1
- test1 : 1
- test2 : 0
- test1 : 0
如果是synchronized修饰的静态方法呢?
Java代码:
- public class TestSynchronized
- {
- public void test1()
- {
- synchronized(TestSynchronized.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 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() { TestSynchronized.test2(); } }, "test2" );
- test1.start();
- test2.start();
- // TestRunnable tr=new TestRunnable();
- // Thread test3=new Thread(tr);
- // test3.start();
- }
- }
- test1 : 4
- test1 : 3
- test1 : 2
- test1 : 1
- test1 : 0
- test2 : 4
- test2 : 3
- test2 : 2
- test2 : 1
- test2 : 0
Java代码:
- public class TestSynchronized
- {
- 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 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() { TestSynchronized.test2(); } }, "test2" );
- test1.start();
- test2.start();
- // TestRunnable tr=new TestRunnable();
- // Thread test3=new Thread(tr);
- // test3.start();
- }
- }
- test1 : 4
- test2 : 4
- test1 : 3
- test2 : 3
- test2 : 2
- test1 : 2
- test2 : 1
- test1 : 1
- test1 : 0
- test2 : 0
到这里,对synchronized的用法已经有了一定的了解。
打个比方:一个object就像一个大房子,大门永远打开。房子里有 很多房间(也就是方法)。这些房间有上锁的(synchronized方法), 和不上锁之分(普通方法)。房门口放着一把钥匙(key),这把钥匙可以打开所有上锁的房间。另外我把所有想调用该对象方法的线程比喻成想进入这房子某个 房间的人。所有的东西就这么多了,下面我们看看这些东西之间如何作用的。在此我们先来明确一下我们的前提条件。该对象至少有一synchronized方法,否则这个key还有啥意义。当然也就不会有我们的这个主题了。一个人想进入某间上了锁的房间,他来到房子门口,看见钥匙在那儿(说明暂时还没有其他人要使用上锁的 房间)。于是他走上去拿到了钥匙,并且按照自己 的计划使用那些房间。注意一点,他每次使用完一次上锁的房间后会马上把钥匙还回去。即使他要连续使用两间上锁的房间,中间他也要把钥匙还回去,再取回来。因此,普通情况下钥匙的使用原则是:“随用随借,用完即还。”这时其他人可以不受限制的使用那些不上锁的房间,一个人用一间可以,两个人用一间也可以,没限制。但是如果当某个人想要进入上锁的房间,他就要跑到大门口去看看了。有钥匙当然拿了就走,没有的话,就只能等了。要是很多人在等这把钥匙,等钥匙还回来以后,谁会优先得到钥匙?Not guaranteed。象前面例子里那个想连续使用两个上锁房间的家伙,他中间还钥匙的时候如果还有其他人在等钥匙,那么没有任何保证这家伙能再次拿到。 (JAVA规范在很多地方都明确说明不保证,像Thread.sleep()休息后多久会返回运行,相同优先权的线程那个首先被执行,当要访问对象的锁被 释放后处于等待池的多个线程哪个会优先得到,等等。我想最终的决定权是在JVM,之所以不保证,就是因为JVM在做出上述决定的时候,绝不是简简单单根据 一个条件来做出判断,而是根据很多条。而由于判断条件太多,如果说出来可能会影响JAVA的推广,也可能是因为知识产权保护的原因吧。SUN给了个不保证 就混过去了。无可厚非。但我相信这些不确定,并非完全不确定。因为计算机这东西本身就是按指令运行的。即使看起来很随机的现象,其实都是有规律可寻。学过 计算机的都知道,计算机里随机数的学名是伪随机数,是人运用一定的方法写出来的,看上去随机罢了。另外,或许是因为要想弄的确太费事,也没多大意义,所 以不确定就不确定了吧。)
再来看看同步代码块。和同步方法有小小的不同。
1.从尺寸上讲,同步代码块比同步方法小。你可以把同步代码块看成是没上锁房间里的一块用带锁的屏风隔开的空间。
2.同步代码块还可以人为的指定获得某个其它对象的key。就像是指定用哪一把钥匙才能开这个屏风的锁,你可以用本房的钥匙;你也可以指定用另一个房子的钥匙才能开,这样的话,你要跑到另一栋房子那儿把那个钥匙拿来,并用那个房子的钥匙来打开这个房子的带锁的屏风。
记住你获得的那另一栋房子的钥匙,并不影响其他人进入那栋房子没有锁的房间。
为什么要使用同步代码块呢?我想应该是这样的:首先对程序来讲同步的部分很影响运行效率,而一个方法通常是先创建一些局部变量,再对这些变量做一些 操作,如运算,显示等等;而同步所覆盖的代码越多,对效率的影响就越严重。因此我们通常尽量缩小其影响范围。
如何做?同步代码块。我们只把一个方法中该同 步的地方同步,比如运算。
另外,同步代码块可以指定钥匙这一特点有个额外的好处,是可以在一定时期内霸占某个对象的key。还记得前面说过普通情况下钥匙的使用原则吗。现在不是普通情况了。你所取得的那把钥匙不是永远不还,而是在退出同步代码块时才还。
还用前面那个想连续用两个上锁房间的家伙打比方。怎样才能在用完一间以后,继续使用另一间呢。用同步代码块吧。先创建另外一个线程,做一个同步代码 块,把那个代码块的锁指向这个房子的钥匙。然后启动那个线程。只要你能在进入那个代码块时抓到这房子的钥匙,你就可以一直保留到退出那个代码块。也就是说 你甚至可以对本房内所有上锁的房间遍历,甚至再sleep(10*60*1000),而房门口却还有1000个线程在等这把钥匙呢。很过瘾吧。