synchronized同步代码块

特性1. synchronized(非this对象x)同步代码块格式进行同步操作时,对象监视器必须为同一个,如果不是同一个,运行结果就是异步调用了,就会交叉运行;

示例1:

package thread;

/**
 * @Auther: 13213
 * @Date: 2020/9/25 17:24
 * @Description:
 */
public class Service {
    private String usernameParam;
    private String passwordParam;
    private String anyString = new String();
    public void setUsernamePassword(String username, String password){
        try {
            synchronized (anyString){
                System.out.println("线程名称为:" + Thread.currentThread().getName() + "在 " + System.currentTimeMillis() + "进入同步块");
                usernameParam = username;
                Thread.sleep(3000);
                passwordParam = password;
                System.out.println("线程名称为:" + Thread.currentThread().getName() + "在 " + System.currentTimeMillis() + "离开同步块");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
package thread;

/**
 * @Auther: 13213
 * @Date: 2020/9/25 21:06
 * @Description:
 */
public class ThreadA extends Thread{
    private Service service;

    public ThreadA(Service service) {
        super();
        this.service = service;
    }

    @Override
    public void run() {
        service.setUsernamePassword("a","aa");
    }
}

 

package thread;

/**
 * @Auther: 13213
 * @Date: 2020/9/25 21:06
 * @Description:
 */
public class ThreadB extends Thread{
    private Service service;

    public ThreadB(Service service) {
        super();
        this.service = service;
    }

    @Override
    public void run() {
        service.setUsernamePassword("b","bb");
    }
}

 

package thread;

/**
 * @Auther: 13213
 * @Date: 2020/9/25 21:10
 * @Description:
 */
public class Run {
    public static void main(String[] args) {
        Service service = new Service();
        ThreadA threadA = new ThreadA(service);
        threadA.setName("A");
        threadA.start();

        ThreadB threadB = new ThreadB(service);
        threadB.setName("B");
        threadB.start();
    }
}

结果:

service.java文件更改如下:

package thread;

/**
 * @Auther: 13213
 * @Date: 2020/9/25 17:24
 * @Description:
 */
public class Service {
    private String usernameParam;
    private String passwordParam;
//    private String anyString = new String();
    public void setUsernamePassword(String username, String password){
        try {
            String anyString = new String();
            synchronized (anyString){
                System.out.println("线程名称为:" + Thread.currentThread().getName() + "在 " + System.currentTimeMillis() + "进入同步块");
                usernameParam = username;
                Thread.sleep(3000);
                passwordParam = password;
                System.out.println("线程名称为:" + Thread.currentThread().getName() + "在 " + System.currentTimeMillis() + "离开同步块");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

结果为:

示例2:

package thread.demo2;

/**
 * @Auther: 13213
 * @Date: 2020/9/25 17:24
 * @Description:
 */
public class Service {
    private String anyString = new String();
    public void a(){
        try {
            synchronized (anyString){
                System.out.println("a begin");
                Thread.sleep(3000);
                System.out.println("a end");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    synchronized public void b(){
        System.out.println("b begin");
        System.out.println("b end");
    }
}
package thread.demo2;

/**
 * @Auther: 13213
 * @Date: 2020/9/25 21:06
 * @Description:
 */
public class ThreadA extends Thread{
    private Service service;

    public ThreadA(Service service) {
        super();
        this.service = service;
    }

    @Override
    public void run() {
        service.a();
    }
}
package thread.demo2;

/**
 * @Auther: 13213
 * @Date: 2020/9/25 21:06
 * @Description:
 */
public class ThreadB extends Thread{
    private Service service;

    public ThreadB(Service service) {
        super();
        this.service = service;
    }

    @Override
    public void run() {
        service.b();
    }
}
package thread.demo2;

/**
 * @Auther: 13213
 * @Date: 2020/9/25 21:10
 * @Description:
 */
public class Run {
    public static void main(String[] args) {
        Service service = new Service();
        ThreadA threadA = new ThreadA(service);
        threadA.setName("A");
        threadA.start();

        ThreadB threadB = new ThreadB(service);
        threadB.setName("B");
        threadB.start();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值