2.2.1synchronized方法的弊端

Posted 成功的路上总是离不开贵人的帮助,名师的指点和小人的刺激。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:\it\jdk1.8\bin\java -Didea.launcher.port=7536 "-Didea.launcher.bin.path=D:\it\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\it\jdk1.8\jre\lib\charsets.jar;D:\it\jdk1.8\jre\lib\deploy.jar;D:\it\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\it\jdk1.8\jre\lib\ext\cldrdata.jar;D:\it\jdk1.8\jre\lib\ext\dnsns.jar;D:\it\jdk1.8\jre\lib\ext\jaccess.jar;D:\it\jdk1.8\jre\lib\ext\jfxrt.jar;D:\it\jdk1.8\jre\lib\ext\localedata.jar;D:\it\jdk1.8\jre\lib\ext\nashorn.jar;D:\it\jdk1.8\jre\lib\ext\sunec.jar;D:\it\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\it\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\it\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\it\jdk1.8\jre\lib\ext\zipfs.jar;D:\it\jdk1.8\jre\lib\javaws.jar;D:\it\jdk1.8\jre\lib\jce.jar;D:\it\jdk1.8\jre\lib\jfr.jar;D:\it\jdk1.8\jre\lib\jfxswt.jar;D:\it\jdk1.8\jre\lib\jsse.jar;D:\it\jdk1.8\jre\lib\management-agent.jar;D:\it\jdk1.8\jre\lib\plugin.jar;D:\it\jdk1.8\jre\lib\resources.jar;D:\it\jdk1.8\jre\lib\rt.jar;F:\springboot\threaddemo\out\production\threaddemo;D:\it\idea\IntelliJ IDEA 2016.3.3\lib\idea_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秒。

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

以上是关于2.2.1synchronized方法的弊端的主要内容,如果未能解决你的问题,请参考以下文章

2.2.3用同步代码块解决同步方法的弊端

java 多线程9 : synchronized锁机制 之 代码块锁

Java多线程5:synchronized锁方法块

Java Thread系列synchronized

创作赢红包Java多线程:synchronized锁方法块

5.并发编程-synchronized 细节说明