多线程-上锁问题

1 定义类

public class Function {
   private static synchronized void staticFunction() {   //  静态方法
        for (int i = 0; i < 4; i++) {
            try {
                System.out.println(Thread.currentThread().getName() + "---" + i);
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private synchronized void ordinaryFunction() { // 普通方法
        for (int i = 0; i < 4; i++) {
            try {
                System.out.println(Thread.currentThread().getName() + "---" + i);
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void localFunction(String key) {
        synchronized (key) {
            for (int i = 0; i < 4; i++) {
                try {
                    System.out.println(Thread.currentThread().getName() + "---" + key + "---" + i);
                    Thread.sleep(100L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2 局部代码块: “sameKey”: 有效; new String(“sameKey”): 失效

	 @Test
    public void localTest() {
        System.out.println("----------localTest---sameKey----------");
        Function function = new Function();
        for (int i = 0; i < 2; i++) {
            new Thread(() -> function.localFunction("sameKey")).start();
        }
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("----------localTest---new String('sameKey')----------");
        for (int i = 0; i < 2; i++) {
            new Thread(() -> function.localFunction(new String("sameKey"))).start();
        }

        try {
            Thread.sleep(100000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
----------localTest---sameKey----------
Thread-1---sameKey---0
Thread-1---sameKey---1
Thread-1---sameKey---2
Thread-1---sameKey---3
Thread-0---sameKey---0
Thread-0---sameKey---1
Thread-0---sameKey---2
Thread-0---sameKey---3
----------localTest---new String('sameKey')----------
Thread-2---sameKey---0
Thread-3---sameKey---0
Thread-2---sameKey---1
Thread-3---sameKey---1
Thread-2---sameKey---2
Thread-3---sameKey---2
Thread-2---sameKey---3
Thread-3---sameKey---3

3 常规方法

	@Test
    public void ordinaryTest(){
        System.out.println("----------ordinaryTest---sameFunction----------");
        Function function = new Function();
        Function function2 = new Function();
        for (int i = 0; i < 2; i++) {
            new Thread(() -> function.ordinaryFunction()).start();
        }
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("----------ordinaryTest---differentFunction----------");
        new Thread(() -> function.ordinaryFunction()).start();
        new Thread(() -> function2.ordinaryFunction()).start();

        try {
            Thread.sleep(100000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
----------ordinaryTest---sameFunction----------
Thread-0---0
Thread-0---1
Thread-0---2
Thread-0---3
Thread-1---0
Thread-1---1
Thread-1---2
Thread-1---3
----------ordinaryTest---differentFunction----------
Thread-2---0
Thread-3---0
Thread-2---1
Thread-3---1
Thread-2---2
Thread-3---2
Thread-2---3
Thread-3---3

4 静态方法

    @Test
    public void staticTest() {
        System.out.println("----------staticTest---staticFunction----------");
        for (int i = 0; i < 2; i++) {
            new Thread(() -> Function.staticFunction()).start();
        }

        try {
            Thread.sleep(100000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
----------staticTest---staticFunction----------
Thread-1---0
Thread-1---1
Thread-1---2
Thread-1---3
Thread-0---0
Thread-0---1
Thread-0---2
Thread-0---3
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值