package com.wkcto.intrinsiclock;
/**
* synchronized同步實(shí)例方法
* 把整個(gè)方法體作為同步代碼塊
* 默認(rèn)的鎖對象是this對象
* Author: 老崔
*/
public class Test05 {
public static void main(String[] args) {
//先創(chuàng)建Test01對象,通過對象名調(diào)用mm()方法
Test05 obj = new Test05();
//一個(gè)線程調(diào)用mm()方法
new Thread(new Runnable() {
@Override
public void run() {
obj.mm(); //使用的鎖對象this就是obj對象
}
}).start();
//另一個(gè)線程調(diào)用mm22()方法
new Thread(new Runnable() {
@Override
public void run() {
obj.mm22(); //使用的鎖對象this也是obj對象, 可以同步
// new Test05().mm22(); //使用的鎖對象this是剛剛new創(chuàng)建的一個(gè)新對象,不是同一個(gè)鎖對象不能同步
}
}).start();
}
//定義方法,打印100行字符串
public void mm(){
synchronized ( this ) { //經(jīng)常使用this當(dāng)前對象作為鎖對象
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + " --> " + i);
}
}
}
//使用synchronized修飾實(shí)例方法,同步實(shí)例方法, 默認(rèn)this作為鎖對象
public synchronized void mm22(){
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + " --> " + i);
}
}
}
package com.wkcto.intrinsiclock;
/**
* synchronized同步靜態(tài)方法
* 把整個(gè)方法體作為同步代碼塊
* 默認(rèn)的鎖對象是當(dāng)前類的運(yùn)行時(shí)類對象, Test06.class, 有人稱它為類鎖
* Author: 老崔
*/
public class Test06 {
public static void main(String[] args) {
//先創(chuàng)建Test01對象,通過對象名調(diào)用mm()方法
Test06 obj = new Test06();
//一個(gè)線程調(diào)用m1()方法
new Thread(new Runnable() {
@Override
public void run() {
obj.m1(); //使用的鎖對象是Test06.class
}
}).start();
//另一個(gè)線程調(diào)用sm2()方法
new Thread(new Runnable() {
@Override
public void run() {
Test06.sm2(); //使用的鎖對象是Test06.class
}
}).start();
}
//定義方法,打印100行字符串
public void m1(){
//使用當(dāng)前類的運(yùn)行時(shí)類對象作為鎖對象,可以簡單的理解為把Test06類的字節(jié)碼文件作為鎖對象
synchronized ( Test06.class ) {
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + " --> " + i);
}
}
}
//使用synchronized修飾靜態(tài)方法,同步靜態(tài)方法, 默認(rèn)運(yùn)行時(shí)類Test06.class作為鎖對象
public synchronized static void sm2(){
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + " --> " + i);
}
}
}
package com.wkcto.intrinsiclock;
/**
* 同步方法與同步代碼塊如何選擇
* 同步方法鎖的粒度粗, 執(zhí)行效率低, 同步代碼塊執(zhí)行效率高
*
* Author: 老崔
*/
public class Test07 {
public static void main(String[] args) {
Test07 obj = new Test07();
new Thread(new Runnable() {
@Override
public void run() {
obj.doLongTimeTask();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
obj.doLongTimeTask();
}
}).start();
}
//同步方法, 執(zhí)行效率低
public synchronized void doLongTimeTask(){
try {
System.out.println("Task Begin");
Thread.sleep(3000); //模擬任務(wù)需要準(zhǔn)備3秒鐘
System.out.println("開始同步");
for(int i = 1; i <= 100; i++){
System.out.println(Thread.currentThread().getName() + "-->" + i);
}
System.out.println("Task end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//同步代碼塊,鎖的粒度細(xì), 執(zhí)行效率高
public void doLongTimeTask2(){
try {
System.out.println("Task Begin");
Thread.sleep(3000); //模擬任務(wù)需要準(zhǔn)備3秒鐘
synchronized (this){
System.out.println("開始同步");
for(int i = 1; i <= 100; i++){
System.out.println(Thread.currentThread().getName() + "-->" + i);
}
}
System.out.println("Task end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}