synchronized初级使用篇

2020-08-30  本文已影响0人  大黑跟小白的日常

此篇主要讲解使用synchronized时同步异步问题

抛出总结:

使用synchronized对资源(对象)进行加锁,多线程并发访问,具体多线程并发操作是同步还是异步执行,就看竞争的资源是否为同一个,或者说抢占的锁是否为同一把。如果竞争的是同一把锁,则同步,为不同的锁则互不干扰(异步)。

测试代码如下,猜猜结果 (共四个)

全猜对者合格

/**
 * @Author G_Y
 * @Date 2020/8/30 20:19
 * @Description: // synchronized初级使用篇
 **/
public class synchronizedLean1 {

    public static void main(String[] args) {

        // 1
        /*final Human h1 = new Human();
        final Human h2 = new Human();
        new Thread(new Runnable() {
            @Override
            public void run() {
                h1.say();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                h2.say();
            }
        }).start();*/

        // 2
        /*final Human h1 = new Human();
        new Thread(new Runnable() {
            @Override
            public void run() {
                h1.say();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                h1.bibi();
            }
        }).start();*/

        // 3
        /*final Human h1 = new Human();
        final Human h2 = new Human();
        new Thread(new Runnable() {
            @Override
            public void run() {
                h1.aiai();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                h2.aiai();
            }
        }).start();*/

        // 4
        final Human h1 = new Human();
        new Thread(new Runnable() {
            @Override
            public void run() {
                h1.cici();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                h1.didi();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                h1.eiei();
            }
        }).start();
    }
}

class Human {
    public static void sleep1ms() {
        for (int i = 1; i <= 10; i++) {
            try {
                TimeUnit.MILLISECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i);
        }
    }

    public synchronized void say() {
        Human.sleep1ms();
    }

    public synchronized void hello() {
        Human.sleep1ms();
    }

    public static synchronized void bibi() {
        Human.sleep1ms();
    }

    public void aiai() {
        synchronized (this.getClass()) {
            Human.sleep1ms();
        }
    }

    public void cici() {
        synchronized (Human.class) {
            Human.sleep1ms();
        }
    }

    public void didi() {
        synchronized ("") {
            Human.sleep1ms();
        }
    }

    public void eiei() {
        synchronized ("abcdefg") {
            Human.sleep1ms();
        }
    }
}
问题: 如上这些多线程在执行方法时是同步还是异步?
上一篇 下一篇

猜你喜欢

热点阅读