多线程基础学习第二篇(多个线程多把锁)

1 篇文章 0 订阅
1 篇文章 0 订阅

多个线程同时竞争多把锁(这是对象上的锁,因为每个对象都有一把锁,所以彼此之间不存在关系)

/**
 * @version V1.0
 * @Description: 多个线程同时竞争多把锁
 * 在静态方法上加synchronize关键字,表示锁定.class类,类一级别的锁(独占.class类)
 * @Modified By:Ming Created in  14:39 2017/3/22
 */
public class MyThread1 {

    private  int num = 0;

    /**
     * static
     **/
    public  synchronized void printNum(String tag) {
        try {
            if (tag.equals("a")) {
                num = 100;
                System.out.println("tag a, set num over!" );
                Thread.sleep(1000);
            } else {
                num = 200;
                System.out.println("tag b, set num over!");
            }
            System.out.println("tag " + tag + ", set num over!" + num);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //注意观察run方法的输出顺序
    public static void main(String[] args) {

        //俩个不同的对象
        final MyThread1 m1 = new MyThread1();
        final MyThread1 m2 = new MyThread1();

        Thread t1 = new Thread(new Runnable() {

            public void run() {
                m1.printNum("a");
            }
        });

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                m2.printNum("b");
            }
        });

        t1.start();
        t2.start();
    }
}

tag a, set num over!
tag b, set num over!
tag b, set num over!200
tag a, set num over!100

关键字synchronized取得的锁都是对象锁,而不是把一段代码(方法)当作锁,所以示例中的哪个线程先执行synchronized关键字的方法,哪个线程就持有该方法所属对象的锁(lock),如果是多个线程,那么线程获得的就是多个不同的锁,他们互不影响(但是有一种情况是相同的锁,即在静态方法上添加synchronized关键字,表示锁定.class,类一级别的锁,独占.class类)

(如果想要他们去竞争同一把锁的)加上静态修饰,变对象锁为类级别的锁

/**
 * @version V1.0
 * @Description: 多个线程同时竞争多把锁
 * 在静态方法上加synchronize关键字,表示锁定.class类,类一级别的锁(独占.class类)
 * @Modified By:Ming Created in  14:39 2017/3/22
 */
public class MyThread1 {

    private static int num = 0;

    /**
     * static
     **/
    public static synchronized void printNum(String tag) {
        try {
            if (tag.equals("a")) {
                num = 100;
                System.out.println("tag a, set num over!" );
                Thread.sleep(1000);
            } else {
                num = 200;
                System.out.println("tag b, set num over!");
            }
            System.out.println("tag " + tag + ", set num over!" + num);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //注意观察run方法的输出顺序
    public static void main(String[] args) {

        //俩个不同的对象
        final MyThread1 m1 = new MyThread1();
        final MyThread1 m2 = new MyThread1();

        Thread t1 = new Thread(new Runnable() {

            public void run() {
                m1.printNum("a");
            }
        });

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                m2.printNum("b");
            }
        });

        t1.start();
        t2.start();
    }
}

tag b, set num over!
tag b, set num over!200
tag a, set num over!
tag a, set num over!100
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值