Java synchronized

@ synchronized
synchronized同步锁

四种方式

1 类锁

主要用在单例

  private static volatile MyManager instance;
  public static MyManager getInstance() {
    if (instance == null) {
        synchronized (MyManager.class) {
        if(instance==null){
            instance = new MyManager();
        }}
    }
    return instance;
}

2 静态方法锁

public static synchronized void addCount3() {
    for (int i = 0; i < 100; i++) {
        count++;
        System.out.println(Thread.currentThread().getName() + "----" + count);
    }
}

静态方法锁锁住的是类,对此类创建的对象都互斥

3 方法锁

public synchronized void addCount2() {
    for (int i = 0; i < 100; i++) {
        count++;
        System.out.println(Thread.currentThread().getName() + "----" + count);
    }
}

这种锁只在当前类中互斥

4代码块锁

public void addCount() {
    synchronized (this) {
        for (int i = 0; i < 100; i++) {
            count++;
            System.out.println(Thread.currentThread().getName() + "----" + count);
        }
    }
}

synchronized (this) 括号中可以是任意对象,锁住的是括号中的对象。

完整代码

private static int count = 0;
private static volatile MyManager instance;
public static MyManager getInstance() {
public class MyManager {
    if (instance == null) {
        synchronized (MyManager.class) {
            instance = new MyManager();
        }
    }
    return instance;
}

public void addCount() {
    synchronized (this) {
        for (int i = 0; i < 100; i++) {
            count++;
            System.out.println(Thread.currentThread().getName() + "----" + count);
        }
    }
}

public synchronized void addCount2() {//只限本类中互斥
    for (int i = 0; i < 100; i++) {
        count++;
        System.out.println(Thread.currentThread().getName() + "----" + count);
    }
}

public static synchronized void addCount3() {
    for (int i = 0; i < 100; i++) {
        count++;
        System.out.println(Thread.currentThread().getName() + "----" + count);
    }
}

@Test
public void main() {

    new Thread(new Run1()).start();
    new Thread(new Run2()).start();

}


class Run1 implements Runnable {
    @Override
    public void run() {
        MyManager.getInstance().addCount2();
    }
}

class Run2 implements Runnable {
    @Override
    public void run() {
        MyManager.getInstance().addCount2();
    }
}}

在其他类中调用方法2

public class SynchronizedTest {
@Test
public void main() {
    new Thread(new Run1()).start();
    new Thread(new Run2()).start();
}
class Run1 implements Runnable {
    @Override
    public void run() {
    
        MyManager.getInstance().addCount2();
    }
}
class Run2 implements Runnable {
    @Override
    public void run() {
        MyManager.getInstance().addCount2();
    }
}}

亲自试试,在SynchronizedTest类中开启俩个线程调用addCount2()并没有互斥。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值