转载自:http://jamesby.iteye.com/blog/41692
第一种情况,synchronized无效
package com.test;
public class ThreadTest extends Thread {
private int threadNo;
public ThreadTest(int threadNo) {
this.threadNo = threadNo;
}
public static void main(String[] args) throws Exception {
for (int i = 1; i < 5; i++) {
new ThreadTest(i).start();
Thread.sleep(1);
}
}
public synchronized void run() {
for (int i = 1; i < 10; i++) {
System.out.println("No." + threadNo + ":" + i);
}
}
}
执行结果:
No.1:1
No.1:2
No.1:3
No.1:4
No.1:5
No.1:6
No.1:7
No.1:8
No.1:9
No.2:1
No.2:2
No.2:3
No.2:4
No.2:5
No.2:6
No.2:7
No.2:8
No.2:9
No.3:1
No.3:2
No.3:3
No.3:4
No.3:5
No.3:6
No.3:7
No.3:8
No.3:9
No.4:1
No.4:2
No.4:3
No.4:4
No.4:5
No.4:6
No.4:7
No.4:8
No.4:9
第二种情况,synchronized有效
package com.test;
public class ThreadTest2 extends Thread {
private int threadNo;
private String lock;
public ThreadTest2(int threadNo, String lock) {
this.threadNo = threadNo;
this.lock = lock;
}
public static void main(String[] args) throws Exception {
String lock = new String("lock");
for (int i = 1; i < 10; i++) {
new ThreadTest2(i, lock).start();
Thread.sleep(1);
}
}
public void run() {
synchronized (lock) {
for (int i = 1; i < 10000; i++) {
System.out.println("No." + threadNo + ":" + i);
}
}
}
}
第三种情况,synchronized有效 package com.test;
public class ThreadTest3 extends Thread {
private int threadNo;
private String lock;
public ThreadTest3(int threadNo, String lock) {
this.threadNo = threadNo;
this.lock = lock;
}
public static void main(String[] args) throws Exception {
String lock = new String("lock");
for (int i = 1; i < 20; i++) {
new ThreadTest3(i, lock).start();
Thread.sleep(1);
}
}
public static synchronized void abc(int threadNo) {
for (int i = 1; i < 10000; i++) {
System.out.println("No." + threadNo + ":" + i);
}
}
public void run() {
abc(threadNo);
}
}
总结:
1、对于同步的方法或者代码块来说,必须获得对象锁才能够进入同步方法或者代码块进行操作;
2、如果采用method级别的同步,则对象锁即为method所在的对象(怎么这么别扭),如果是静态方法,对象锁即指method所在的Class对象(唯一);
3、对于代码块则,对象锁即指synchronized(abc)中的abc;
4、因为第一种情况,对象锁即为每一个线程对象,因此有多个,所以同步失效,第二种共用同一个对象锁lock,因此同步生效,第三个因为是static因此对象锁为ThreadTest3的class 对象,因此同步生效。
如上述正确,则同步有两种方式,代码块和方法
如果是代码块,则对象锁自己指定,一般有些代码为synchronized(this)只有在单态模式才生效;
如果是方法,则分静态和非静态两种。
静态方法则一定会同步,非静态方法需在单例模式才生效,推荐用静态方法(不用担心是否单例)。
--------------分割线----------------------
补充:
1.synchronized是锁对象,根本不存在锁代码块的概念
2.synchronized锁定的对象
(1)synchronized(obj){}-> 这个当然是锁obj
(2)public void synchronized function(){} -> 这个锁的是function这个方法所在的对象,相当于
public void function() {
synchronized(this){}
}
(3)public static void synchronized function() ->对于静态方法,锁定的是当前类(假设是类TestClass)的Class对象,相当于
public static void function() {
synchronized(TestClass.class) {}
}