/**
* @author wuyoushan
* @date 2017/3/29.
*/
public class MyObject {
public void methodA(){
try {
System.out.println("begin methodA threadName=" +
Thread.currentThread().getName());
Thread.sleep(1000);
System.out.println("end");
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
/**
* @author wuyoushan
* @date 2017/4/4.
*/
public class ThreadA extends Thread{
private MyObject object;
public ThreadA(MyObject object){
super();
this.object=object;
}
@Override
public void run() {
super.run();
object.methodA();
}
}
/**
* @author wuyoushan
* @date 2017/4/4.
*/
public class ThreadB extends Thread{
private MyObject object;
public ThreadB(MyObject object){
super();
this.object=object;
}
@Override
public void run() {
super.run();
object.methodA();
}
}
/**
* @author wuyoushan
* @date 2017/3/20.
*/
public class Run {
public static void main(String[] args){
MyObject object=new MyObject();
ThreadA threadA=new ThreadA(object);
threadA.setName("A");
ThreadB threadB=new ThreadB(object);
threadB.setName("B");
threadA.start();
threadB.start();
}
}
程序運(yùn)行候的結(jié)果為:
begin methodA threadName=A
begin methodA threadName=B
end
end
更改MyObject.java中的代碼:
/**
* @author wuyoushan
* @date 2017/3/29.
*/
public class MyObject {
synchronized public void methodA(){
try {
System.out.println("begin methodA threadName=" +
Thread.currentThread().getName());
Thread.sleep(1000);
System.out.println("end");
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
更改后得運(yùn)行結(jié)果為:
begin methodA threadName=A
end
begin methodA threadName=B
end
通過上面的實(shí)驗(yàn)結(jié)論,調(diào)用關(guān)鍵字synchronized聲明的方法一定是排隊(duì)運(yùn)行的。另外需要牢牢記住“共享”這兩個(gè)字,只有共享資源的讀寫訪問才需要同步化,如果不是共享資源,那么根本就沒有同步的必要。
那其他的方法被調(diào)用時(shí)會(huì)是什么效果呢?如何查看到Lock鎖對(duì)象效果呢?
/**
* @author wuyoushan
* @date 2017/3/29.
*/
public class MyObject {
synchronized public void methodA(){
try {
System.out.println("begin methodA threadName=" +
Thread.currentThread().getName());
Thread.sleep(1000);
System.out.println("end endTime="+System.currentTimeMillis());
}catch (InterruptedException e){
e.printStackTrace();
}
}
public void methodB(){
try {
System.out.println("begin methodB threadName=" +
Thread.currentThread().getName()+"begin time"+
System.currentTimeMillis());
Thread.sleep(1000);
System.out.println("end");
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
/**
* @author wuyoushan
* @date 2017/4/4.
*/
public class ThreadA extends Thread{
private MyObject object;
public ThreadA(MyObject object){
super();
this.object=object;
}
@Override
public void run() {
super.run();
object.methodA();
}
}
/**
* @author wuyoushan
* @date 2017/4/4.
*/
public class ThreadB extends Thread{
private MyObject object;
public ThreadB(MyObject object){
super();
this.object=object;
}
@Override
public void run() {
super.run();
object.methodB();
}
}
/**
* @author wuyoushan
* @date 2017/3/20.
*/
public class Run {
public static void main(String[] args){
MyObject object=new MyObject();
ThreadA threadA=new ThreadA(object);
threadA.setName("A");
ThreadB threadB=new ThreadB(object);
threadB.setName("B");
threadA.start();
threadB.start();
}
}
程序的運(yùn)行結(jié)果為:
begin methodA threadName=A
begin methodB threadName=Bbegin time1491525491404
end
end endTime=1491525492404
通過上面的實(shí)驗(yàn)可以得知,雖然線程A先持有了object對(duì)象的鎖,但線程B完全可以異步調(diào)用非synchronized類型的方法。
如果將MyObject.java文件中的methodB()方法前加上synchronized關(guān)鍵字,代碼如下:
/**
* @author wuyoushan
* @date 2017/3/29.
*/
public class MyObject {
synchronized public void methodA(){
try {
System.out.println("begin methodA threadName=" +
Thread.currentThread().getName());
Thread.sleep(1000);
System.out.println("end endTime="+System.currentTimeMillis());
}catch (InterruptedException e){
e.printStackTrace();
}
}
synchronized public void methodB(){
try {
System.out.println("begin methodB threadName=" +
Thread.currentThread().getName()+"begin time"+
System.currentTimeMillis());
Thread.sleep(1000);
System.out.println("end");
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
程序的運(yùn)行結(jié)果為:
begin methodA threadName=A
end endTime=1491525875727
begin methodB threadName=Bbegin time1491525875727
end
此實(shí)驗(yàn)的結(jié)論是:
- A線程先持有object對(duì)象的Lock鎖,B線程可以以異步的方式調(diào)用object對(duì)象中的非synchronized類型的方法。
- A線程先持有object對(duì)象的Lock鎖,B線程如果在這時(shí)調(diào)用object對(duì)象中的synchronized類型的方法則需等待,也就是同步。
摘選自 java多線程核心編程技術(shù)-2.1.4