synchronized同步代码块

2.4. synchronized同步代码块

2.4.1. 同步代码块的使用方法

1)使用方法说明:
在多个线程共同访问的代码放在synchronized(){ 代码块 } 中的代码块位置处。
2)样例程序:

public class UsingMethod {
   
    public static void main(String[] args) {
   
        Service service = new Service();
        UsingMethodThread1 thread1 = new UsingMethodThread1(service);
        thread1.setName("A");
        UsingMethodThread2 thread2 = new UsingMethodThread2(service);
        thread2.setName("B");
        thread1.start();
        thread2.start();
    }
}


class Service{
   
    public void foo(){
   
        try {
   
            synchronized (this){
   
                System.out.println("线程" + Thread.currentThread().getName() + "开始");
                Thread.sleep(2000);
                System.out.println("线程" + Thread.currentThread().getName() + "结束");
            }
        } catch (InterruptedException e) {
   
            e.printStackTrace();
        }
    }
}

class UsingMethodThread1 extends Thread{
   
    private Service service;

    public UsingMethodThread1(Service service){
   
        this.service = service;
    }

    @Override
    public void run(){
   
        this.service.foo();
    }
}

class UsingMethodThread2 extends Thread{
   
    private Service service;

    public UsingMethodThread2(Service service){
   
        this.service = service;
    }

    @Override
    public void run(){
   
        this.service.foo();
    }
}
2.4.2. 锁对象
2.4.2.1. this作对象锁

1)功能描述:
在多线程中使用同步synchronized(this)同步代码块时,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对这个object的其他synchronized(this)同步的访问会被阻塞,说明synchronized使用的对象锁是同一个。
2)样例程序

public class Synchronism {
   
    public static void main(String[] args) throws InterruptedException {
   
        SynchronismService service = new SynchronismService();
        SynchronismThreadA threadA = new SynchronismThreadA(service);
        threadA.setName("A");
        threadA.start();
        Thread.sleep(1000);
        SynchronismThreadB threadB = new SynchronismThreadB(service);
        threadB.setName("B");
        threadB.start();
    }
}

class SynchronismService{
   
    public void foo1(){
   
        try{
   
            synchronized (this){
   
                System.out.println("线程" + Thread.currentThread().getName() + "中foo1启动");
                Thread.sleep(1000);
                System.out.println("线程" + Thread.currentThread().getName() + "中foo1结束");
            }
        }catch (InterruptedException e){
   
            e.printStackTrace();
        }
    }

    public void foo2(){
   
        synchronized (this){
   
            System.out.println("线程" + Thread.currentThread().getName() + "中foo2启动");
            System.out.println("线程" + Thread.currentThread().getName() + "中foo2结束");
        }
    }
}

class SynchronismThreadA extends Thread{
   
    private SynchronismService service;

    public SynchronismThreadA(SynchronismService service){
   
        this.service = service;
    }

    @Override
    public void run(){
   
        this.service.foo1();
    }
}

class SynchronismThreadB extends Thread{
   
    private SynchronismService service;

    public SynchronismThreadB(SynchronismService service){
   
        this.service = service;
    }

    @Override
    public void run(){
   
        this.service.foo2();
    }
}
2.4.2.2. 当前对象作对象锁

1)基本描述
Synchronized同步代码块和synchronized同步方法一样,锁定的都是当前对象。
2)样例程序

public class LockObject {
   
    public static void main(String[] args) throws InterruptedException {
   
        LockObjectService service = new LockObjectService();
        LockObjectThreadA threadA = new LockObjectThreadA(service);
        threadA.setName("A");
        threadA.start();
        Thread.sleep(1000);
        LockObjectThreadB threadB = new LockObjectThreadB(service);
        threadB.setName("B");
        threadB.start();
    }
}

class LockObjectService{
   
    public void foo1(){
   
        try{
   
            synchronized (this){
   
                System.out.println("线程" + Thread.currentThread().getName() + "中foo1启动");
                Thread.sleep(1000);
                System.out.println("线程" + Thread.currentThread().getName() + "中foo1结束");
            }
        }catch (InterruptedException e){
   
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值