java并发(4)-synchronized

java并发(4)-synchronized

synchronized主要是用于解决共享资源竞争的问题。因为在多线程开发里面,如果存在一个共享资源只允许在一个特定时刻只能一个任务访问它。那么就需要对该资源加锁,让其他人在这个时刻无法访问该资源。只有等待另一个人释放了才可以下一个人使用。

static synchronized 类锁

下面运行一段代码:

public class MainThread {
    public static void main(String[] args) {
        MyThread
            aMyThread = new MyThread("A-MyThread",1),
            bMyThread = new MyThread("B-MyThread",2);
        aMyThread.start();
        bMyThread.start();
        System.out.println("Main Thread run Completed!");
    }
}
class MyThread extends Thread {
    private  String name;
    private int call;
    public MyThread(String name ,int call) {
        this.name = name;
        this.call = call;
    }
    @Override
    public void run() {
        switch (call) {
            case 1:
                callA();
                break;
            case 2:
                callB();
                break;
        }
    }
    private static synchronized void callA() {
        System.out.println("callA is runing");
        try {
            sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("callA had run");
    }
    private static synchronized void callB() {
        System.out.println("callB is runing");
        try {
            sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("callB had run");
    }
}

输入的结果如下:

callA is runing
Main Thread run Completed!
//等待10秒
callA had run
callB is runing
//等待10秒
callB had run

由上面的结果可以看出,如果是static synchronized的话,那么锁的就是对象。该对象的方法如果添加了static synchronized,那么无论是那个对象,对这些方法的访问都需要等待释放锁。

对象锁

如果把上面代码中的static去掉。那么运行的结果如下:

Main Thread run Completed!
callA is runing
callB is runing
//等待10秒
callA had run
callB had run

这样可以看到,由于两个线程是不同对象,而且调用的是不同的方法。所以不会互相影响。拓展一下,两个不同对象,即使访问的是同一个synchronized方法,也不会互相影响。
如下代码:

public class MainThread {
    public static void main(String[] args) {
        MainThread mt = new MainThread();
        MyThread
            aMyThread = new MyThread("A-MyThread",mt),
            bMyThread = new MyThread("B-MyThread",mt);
        aMyThread.start();
        bMyThread.start();
        System.out.println("Main Thread run Completed!");
    }

    public synchronized void callC(String threadname) {
        System.out.println("callC is runing:" + threadname);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("callC had run:" + threadname);
    }
}

class MyThread extends Thread {
    private  String name;
    private MainThread mt;
    public MyThread(String name ,MainThread mt) {
        this.name = name;
        this.mt = mt;
    }
    @Override
    public void run() {
        mt.callC(name);
    }
}

运行结果如下:

Main Thread run Completed!
callC is runing:A-MyThread
callC had run:A-MyThread
//等待10秒
callC is runing:B-MyThread
callC had run:B-MyThread

只有当两个不同线程访问的是同一个对象的synchronized方法的时候,才会需要等待释放锁。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小明是我的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值