synchronized方法,synchronized靜態方法,synchronized(this),synchronized(類),synchronized(變量)之間的區別
先貼代碼
/**
* Created by sunshanpeng on 2016/9/27.
*/
public class ThreadTest {
public static final String Lock = "lock";
public final Object obj = new Object();
synchronized void test1() throws InterruptedException {
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("test1="+i);
}
}
synchronized static void test2() throws InterruptedException {
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("test2=" + i);
}
}
void test3() throws InterruptedException {
synchronized (this) {
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("test3=" + i);
}
}
}
void test4() throws InterruptedException {
synchronized (this.getClass()) {
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("test4=" + i);
}
}
}
void test5() throws InterruptedException {
synchronized (Lock) {
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("test5=" + i);
}
}
}
void test6() throws InterruptedException {
synchronized (obj) {
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("test6=" + i);
}
}
}
}
一、同一個對象的同一個同步方法是同步的
這個毋庸置疑,不多解釋
二、同一個對象的synchronized方法和synchronized(this)塊是同步的
方法test1和test3同步
這兩個方法的鎖都是加在當前對象上
三、類的static方法和synchronized(該類)塊是同步的
方法test2和test4同步
這兩個方法的鎖都是加在ThreadTest類上面,方法test4的synchronized (this.getClass())塊換成synchronized (ThreadTest.class),結果是一樣的,但是如果換成比如synchronized (String.class)就不能同步了
因爲鎖是加在類上面,所以不僅相同類的這兩種方法是同步的,同一個類的不同對象之間的這兩種方法也是同步的
四、第二點的兩種方法和第三點的兩種方法不同步
test1和test2不同步,test1和test4不同步
test3和test2不同步,test3和test4不同步
五、synchronized(變量)跟前面四種都不同步
因爲鎖加的目標不一樣,既不是當前類也不是當前對象。
synchronized (obj)和synchronized (this)的用法基本一致,對當前對象加鎖。
而synchronized (Lock)除了當前對象以外,本類的其他新建對象也會同步加鎖,甚至於其他類如果調用的synchronized (ThreadTest.Lock)也會同步。
比如:
class ThreadTest2{
void test7() throws InterruptedException {
synchronized (ThreadTest.Lock) {
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("test7=" + i);
}
}
}
}
ThreadTest2的test7方法跟ThreadTest的test5方法是同步的;
測試代碼:
public static void main(String[] args) throws Exception{
final ThreadTest t1 = new ThreadTest();
final ThreadTest t2 = new ThreadTest();
new Thread(){
public void run() {
try {
t1.test5();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
new Thread(){
public void run() {
try {
new ThreadTest2().test7();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
語言組織的不是很好,因爲網上沒有其他相關文獻,所以寫一篇來拋磚引玉。