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

2.2.2synchronized同步代码块的使用

时间:2017-12-06 23:50:16      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:star   res   add   执行   cep   pack   catch   PFS   sse   

当两个并发线程访问同一个对象object中的synchronized(this)同步代码块时,一段时间内只能有一个线程执行,另一个线程必须等待期执行完才能执行。

package com.cky.bean;

/**
 * Created by chenkaiyang on 2017/12/6.
 */
public class ObjectService {
    public void serviceMethodA(){
        try {
            synchronized (this) {
                System.out.println("begin="+System.currentTimeMillis());
                Thread.sleep(2000);
                System.out.println("end="+System.currentTimeMillis());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}
package com.cky.thread;

import com.cky.bean.ObjectService;

/**
 * Created by chenkaiyang on 2017/12/5.
 */
public class ThreadA extends Thread{
    private ObjectService service;
    public ThreadA(ObjectService service) {
        super();
        this.service = service;
    }

    @Override
    public void run() {
        super.run();
        service.serviceMethodA();
    }
}
package com.cky.thread;

import com.cky.bean.ObjectService;

/**
 * Created by chenkaiyang on 2017/12/5.
 */
public class ThreadB extends  Thread{
    private ObjectService service;
    public ThreadB(ObjectService service) {
        super();
        this.service = service;
    }

    @Override
    public void run() {
        super.run();
        service.serviceMethodA();
    }
}
package com.cky.test;

import com.cky.bean.ObjectService;
import com.cky.thread.ThreadA;
import com.cky.thread.ThreadB;

/**
 * Created by chenkaiyang on 2017/12/6.
 */
public class Test2 {
    public static void main(String[] args) {
        ObjectService objectService = new ObjectService();
        ThreadA a = new ThreadA(objectService);
        a.setName("a");
        ThreadB b = new ThreadB(objectService);
        b.setName("b");
        a.start();
        b.start();

    }
}
D:\it\jdk1.8\bin\java -Didea.launcher.port=7535 "-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.Test2
begin=1512567935564
end=1512567937564
begin=1512567937564
end=1512567939564

结果表明:方法执行是同步的,但是效果没有提高,下面模拟提高效率

2.2.2synchronized同步代码块的使用

标签:star   res   add   执行   cep   pack   catch   PFS   sse   

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

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