zoukankan      html  css  js  c++  java
  • 线程八锁

    所谓的“线程八锁”,其实就是考察 synchronized 锁住的是哪个对象
    情况1:12 或 21

    锁住的为同一对象,2个线程都有可能执行

    @Slf4j(topic = "c.Number")
    class Number{
    public synchronized void a() {
    log.debug("1");
    }
    public synchronized void b() {
    log.debug("2");
    }
    }
    public static void main(String[] args) {
    Number n1 = new Number();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n1.b(); }).start();
    }
    

    情况2:1s后12,或 2 1s后 1

    锁住的为同一对象,2个线程都有可能执行

    @Slf4j(topic = "c.Number")
    class Number{
    public synchronized void a() {
    sleep(1);
    log.debug("1");
    }
    public synchronized void b() {
    log.debug("2");
    }
    }
    public static void main(String[] args) {
    Number n1 = new Number();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n1.b(); }).start();
    }
    

    情况3:3 1s 12 或 23 1s 1 或 32 1s 1

    锁住的为同一对象,3个线程都有可能执行

    class Number{
    public synchronized void a() {
    sleep(1);
    log.debug("1");
    }
    public synchronized void b() {
    log.debug("2");
    }
    public void c() {
    log.debug("3");
    }
    }
    public static void main(String[] args) {
    Number n1 = new Number();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n1.b(); }).start();
    new Thread(()->{ n1.c(); }).start();
    }
    

    情况4:2 1s 后 1

    锁住的不为同一对象,不存在锁竞争,第二个线程先执行。

    @Slf4j(topic = "c.Number")
    class Number{
    public synchronized void a() {
    sleep(1);
    log.debug("1");
    }
    public synchronized void b() {
    log.debug("2");
    }
    }
    public static void main(String[] args) {
    Number n1 = new Number();
    Number n2 = new Number();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n2.b(); }).start();
    }
    

    情况5:2 1s 后 1

    锁住的不为同一对象,不存在锁竞争,第二个线程先执行,第一个锁的是类,第二个是对象

    @Slf4j(topic = "c.Number")
    class Number{
    public static synchronized void a() {
    sleep(1);
    log.debug("1");
    }
    public synchronized void b() {
    log.debug("2");
    }
    }
    public static void main(String[] args) {
    Number n1 = new Number();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n1.b(); }).start();
    }
    

    情况6:1s 后12, 或 2 1s后 1

    锁住的为同一对象,2个线程都有可能执行

    @Slf4j(topic = "c.Number")
    class Number{
    public static synchronized void a() {
    sleep(1);
    log.debug("1");
    }
    public static synchronized void b() {
    log.debug("2");
    }
    }
    public static void main(String[] args) {
    Number n1 = new Number();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n1.b(); }).start();
    }
    

    情况7:2 1s 后 1

    锁住的不为同一对象,不存在锁竞争,第二个线程先执行

    @Slf4j(topic = "c.Number")
    class Number{
    public static synchronized void a() {
    sleep(1);
    log.debug("1");
    }
    public synchronized void b() {
    log.debug("2");
    }
    }
    public static void main(String[] args) {
    Number n1 = new Number();
    Number n2 = new Number();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n2.b(); }).start();
    }
    

    情况8:1s 后12, 或 2 1s后 1

    锁住的为同一对象,2个线程都有可能执行

    class Number{
    public static synchronized void a() {
    sleep(1);
    log.debug("1");
    }
    public static synchronized void b() {
    log.debug("2");
    }
    }
    public static void main(String[] args) {
    Number n1 = new Number();
    Number n2 = new Number();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n2.b(); }).start();
    }
    
  • 相关阅读:
    安全工具
    WebRTC媒体协商及实践
    流媒体协议介绍(rtp/rtcp/rtsp/rtmp/mms/hls)
    基于 WebRTC 技术的实时通信服务开发实践
    实时音视频互动系列(下):基于 WebRTC 技术的实战解析
    WebRTC基于浏览器的开发
    webRtc+websocket多人视频通话
    Android IOS WebRTC 音视频开发总结(四九) ffmpeg介绍
    Android IOS WebRTC 音视频开发总结(二五) webrtc优秀资源汇总
    The 3n + 1 problem UVA 100
  • 原文地址:https://www.cnblogs.com/dalianpai/p/14205378.html
Copyright © 2011-2022 走看看