結論:
- synchronized(SynchronizedTest.class)鎖加在類上,若有多個類的實例對象,則同一時刻只能由一個類的實例對象(擁有t1的線程th1)獲取到該類上的鎖,其他類的實例對象(擁有t2的線程th2)需要等待
- synchronized void synchronizedMethod() 鎖加在普通方法上,同一時刻多個實例對象訪問到該方法時,每個實例對象都可以進行方法的執行
- synchronized void synchronizedMethod() 鎖加在當前實例對象上,多個線程擁有同一個實例對象,同一時刻只能有一個擁有該實例對象的線程獲得該方法鎖進而執行方法,其他擁有該實例對象的線程需要等待
代碼:
```java
package unittest.thread;
public class SynchronizedTest {
//鎖住class對象
// public static void synchronizedStatic(){
public void synchronizedStatic(){
synchronized(SynchronizedTest.class){
for(int i = 0 ; i < 10 ; i ++){
System.out.println(Thread.currentThread().getName() + i + ":synchronizedStatic");
try {Thread.sleep(100);} catch (InterruptedException e) { e.printStackTrace();}
}
}
}
//鎖住方法,lock標記打在該實例上
public synchronized void synchronizedMethod(){
for(int i = 0 ; i < 10 ; i ++){
System.out.println(Thread.currentThread().getName() + i + ":synchronizedMethod");
try {Thread.sleep(100);} catch (InterruptedException e) { e.printStackTrace();}
}
}
//不會有影響,正常調用
public void synchronizedMethod2WithNosynchronized(){System.out.println("synchronizedMethod2WithNosynchronized"); }
//synchronizedMethod 已經鎖住實例,再加鎖不成功
public void synchronizedMethod2(){
synchronized( this ){
for(int i = 0 ; i < 10 ; i ++){
System.out.println(Thread.currentThread().getName() + i + ":synchronizedMethod2");
try {Thread.sleep(100);} catch (InterruptedException e) { e.printStackTrace();}
}
}
}
//synchronizedMethod 已經鎖住實例, 再加鎖不成功
public void synchronizedMethod3(){synchronized( this ){ System.out.println("synchronizedMethod3"); } }
public static void main(String[] args) throws InterruptedException {
final SynchronizedTest t= new SynchronizedTest();
final SynchronizedTest t1= new SynchronizedTest();
final SynchronizedTest t2= new SynchronizedTest();
//synchronized(SynchronizedTest.class)鎖加在類上,若有多個類的實例對象,則同一時刻只能由一個類的實例對象(擁有t1的線程th1)獲取到該類上的鎖,其他類的實例對象(擁有t2的線程th2)需要等待
//Thread th1 = new Thread( new Runnable(){ @Override public void run() { SynchronizedTest.synchronizedStatic(); } } ,"線程th1-"); th1.start();
//Thread th2 = new Thread( new Runnable(){ @Override public void run() { SynchronizedTest.synchronizedStatic(); } } ,"線程th2-"); th2.start();
//Thread th1 = new Thread( new Runnable(){ @Override public void run() { t1.synchronizedStatic(); } } ,"線程th1-"); th1.start();
//Thread th2 = new Thread( new Runnable(){ @Override public void run() { t2.synchronizedStatic(); } } ,"線程th2-"); th2.start();
//synchronized void synchronizedMethod() 鎖加在普通方法上,同一時刻多個實例對象訪問到該方法時,每個實例對象都可以進行方法的執行
//Thread th3 = new Thread( new Runnable(){ @Override public void run() {t1.synchronizedMethod();} } ,"線程th3-"); th3.start();
//Thread th4 = new Thread( new Runnable(){ @Override public void run() { t2.synchronizedMethod();} } ,"線程th4-"); th4.start();
//synchronized void synchronizedMethod() 鎖加在普通方法上,多個線程擁有同一個實例對象,同一時刻只能有一個擁有該實例對象的線程獲得該方法鎖進而執行方法,其他擁有該實例對象的線程需要等待
//Thread th3 = new Thread( new Runnable(){ @Override public void run() {t.synchronizedMethod();} } ,"線程th3-"); th3.start();
//Thread th4 = new Thread( new Runnable(){ @Override public void run() { t.synchronizedMethod();} } ,"線程th4-"); th4.start();
//synchronized void synchronizedMethod() 鎖加在當前實例對象上,多個線程擁有同一個實例對象,同一時刻只能有一個擁有該實例對象的線程獲得該方法鎖進而執行方法,其他擁有該實例對象的線程需要等待
//Thread th5 = new Thread( new Runnable(){ @Override public void run() { t.synchronizedMethod2();} } ,"線程th5-"); th5.start();
//Thread th6 = new Thread( new Runnable(){ @Override public void run() { t.synchronizedMethod2();} } ,"線程th6-"); th6.start();
/*
t.synchronizedMethod2WithNosynchronized();
t.synchronizedMethod3();
*/
}
}