码迷,mamicode.com
首页 > 其他好文 > 详细

2.2.1synchronized方法的弊端

时间:2017-12-07 00:01:12      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:返回   data   dea   mil   locale   同步   demo   com   class   

缺陷:用关键字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方法的弊端

标签:返回   data   dea   mil   locale   同步   demo   com   class   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!