2.2.1synchronized方法的弊端

缺陷:用关键字synchronized声明方法是有弊端的,譬如A线程调用同步方法执行一个长时间的任务,那么B线程则必须等待较长的时间,

解决方法:使用synchronized同步语句块

 1 package com.cky.utils;
 2 
 3 /**
 4  * Created by chenkaiyang on 2017/12/6.
 5  */
 6 public class Task {
 7     private String getData1;
 8     private String getData2;
 9     public synchronized  void doLongTimeTask() {
10         try {
11             System.out.println("begin task");
12             Thread.sleep(3000);
13             getData1 = "长时间处理任务后从远程返回的值 1 ThreadName="+ Thread.currentThread().getName();
14             getData2 = "长时间处理任务后从远程返回的值 2 ThreadName="+ Thread.currentThread().getName();
15             System.out.println(getData1);
16             System.out.println(getData2);
17             System.out.println("end");
18         } catch (InterruptedException e) {
19             e.printStackTrace();
20         }
21     }
22 }
package com.cky.utils;

/**
 * Created by chenkaiyang on 2017/12/6.
 */
public class CommonUtils {
    public static long beginTime1;
    public static long endTime1;
    public static long beginTime2;
    public static long endTime2;
}
package com.cky.thread;

import com.cky.utils.CommonUtils;
import com.cky.utils.Task;

/**
 * Created by chenkaiyang on 2017/12/6.
 */
public class MyThreadA extends  Thread{
    private Task task;
    public MyThreadA (Task task) {
        this.task = task;
    }
    @Override
    public void run() {
        super.run();
        CommonUtils.beginTime1 = System.currentTimeMillis();
        task.doLongTimeTask();
        CommonUtils.endTime1 = System.currentTimeMillis();
    }
}
package com.cky.thread;

import com.cky.utils.CommonUtils;
import com.cky.utils.Task;

/**
 * Created by chenkaiyang on 2017/12/6.
 */
public class MyThreadB extends Thread{
    private Task task;
    public MyThreadB (Task task) {
        this.task = task;
    }
    @Override
    public void run() {
        super.run();
        CommonUtils.beginTime2 = System.currentTimeMillis();
        task.doLongTimeTask();
        CommonUtils.endTime2 = System.currentTimeMillis();
    }
}
package com.cky.test;

import com.cky.thread.MyThreadA;
import com.cky.thread.MyThreadB;
import com.cky.utils.CommonUtils;
import com.cky.utils.Task;

/**
 * Created by chenkaiyang on 2017/12/2.
 */
public class Test {
    public static void main(String[] args){
        Task task = new Task();
        MyThreadA myThreadA = new MyThreadA(task);
        myThreadA.start();
        MyThreadB myThreadB = new MyThreadB(task);
        myThreadB.start();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long beginTime = (CommonUtils.beginTime2 <CommonUtils.beginTime1?CommonUtils.beginTime2:CommonUtils.beginTime1);
        long endTime = (CommonUtils.endTime2> CommonUtils.endTime1?CommonUtils.endTime2:CommonUtils.endTime1);
        System.out.println("耗时"+ (endTime- beginTime)/1000);

    }
}
D:itjdk1.8injava -Didea.launcher.port=7536 "-Didea.launcher.bin.path=D:itideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "D:itjdk1.8jrelibcharsets.jar;D:itjdk1.8jrelibdeploy.jar;D:itjdk1.8jrelibextaccess-bridge-64.jar;D:itjdk1.8jrelibextcldrdata.jar;D:itjdk1.8jrelibextdnsns.jar;D:itjdk1.8jrelibextjaccess.jar;D:itjdk1.8jrelibextjfxrt.jar;D:itjdk1.8jrelibextlocaledata.jar;D:itjdk1.8jrelibext
ashorn.jar;D:itjdk1.8jrelibextsunec.jar;D:itjdk1.8jrelibextsunjce_provider.jar;D:itjdk1.8jrelibextsunmscapi.jar;D:itjdk1.8jrelibextsunpkcs11.jar;D:itjdk1.8jrelibextzipfs.jar;D:itjdk1.8jrelibjavaws.jar;D:itjdk1.8jrelibjce.jar;D:itjdk1.8jrelibjfr.jar;D:itjdk1.8jrelibjfxswt.jar;D:itjdk1.8jrelibjsse.jar;D:itjdk1.8jrelibmanagement-agent.jar;D:itjdk1.8jrelibplugin.jar;D:itjdk1.8jrelib
esources.jar;D:itjdk1.8jrelib
t.jar;F:springboot	hreaddemooutproduction	hreaddemo;D:itideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
begin task
长时间处理任务后从远程返回的值 1 ThreadName=Thread-0
长时间处理任务后从远程返回的值 2 ThreadName=Thread-0
end
begin task
长时间处理任务后从远程返回的值 1 ThreadName=Thread-1
长时间处理任务后从远程返回的值 2 ThreadName=Thread-1
end
耗时6

程序运行了大约6秒。

需要使用同步代码块来解决。

原文地址:https://www.cnblogs.com/edison20161121/p/7989467.html