Java 多线程编程 力扣实题

多线程编程实例

了解内存模型、线程通信和线程安全之后,对多线程编程已经有了理论上的认知,现在来实战一下。所有题目在https://leetcode.cn/problemset/concurrency/。

按序打印

题干描述

给你一个类:

public class Foo {

public void first() { print("first"); }

public void second() { print("second"); }

public void third() { print("third"); }

}

三个不同的线程 A、B、C 将会共用一个 Foo 实例。

  • 线程 A 将会调用 first() 方法
  • 线程 B 将会调用 second() 方法
  • 线程 C 将会调用 third() 方法

请设计修改程序,以确保 second() 方法在 first() 方法之后被执行,third() 方法在 second() 方法之后被执行。

提示:

  • 尽管输入中的数字似乎暗示了顺序,但是我们并不保证线程在操作系统中的调度顺序。
  • 你看到的输入格式主要是为了确保测试的全面性。

示例 1:

输入: nums = [1,2,3]
输出: "firstsecondthird"
解释:
有三个线程会被异步启动。输入 [1,2,3] 表示线程 A 将会调用 first() 方法,线程 B 将会调用 second() 方法,线程 C 将会调用 third() 方法。正确的输出是 "firstsecondthird"。

示例 2:

输入: nums = [1,3,2]
输出: "firstsecondthird"
解释:
输入 [1,3,2] 表示线程 A 将会调用 first() 方法,线程 B 将会调用 third() 方法,线程 C 将会调用 second() 方法。正确的输出是 "firstsecondthird"。

提示:

  • nums[1, 2, 3] 的一组排列
解题思路

每个线程打印一个单词,但要控制每个线程打印时的次序。通过阻塞和非阻塞的方式都可以达到这个效果。

同步阻塞

通过锁和标识符控制,三个打印方法使用同一个对象锁。同一时间只能有一个获取到锁对象,如果是first线程则直接打印,然后更改first标识为true,调用notifyAll()方法唤醒等待线程;其它线程根据标识符判断是否打印,若不打印则调用wait方法,释放锁。

class Foo {

    private boolean first = false;
    private boolean second = false;

    public Foo() {

    }

    public void first(Runnable printFirst) throws InterruptedException {

        synchronized (this) {
            printFirst.run();
            first = true;
            this.notifyAll();
        }
    }

    public void second(Runnable printSecond) throws InterruptedException {

        synchronized (this) {
            while (!first) {
                this.wait();
            } 

            printSecond.run();
            second = true;
            this.notifyAll();
        }
        // 这种写法的问题在于没搞明白线程间的通信后,被唤醒后,是从wait()方法返回,
        // 不是重新执行synchronized 区域代码
        /*if (first) {
        printSecond.run();
            second = true;
            this.notifyAll();
        } else {
            this.wait();
        }*/
    }

    public void third(Runnable printThird) throws InterruptedException {

        synchronized (this) {
            while (!second) {
                this.wait();
            } 

            printThird.run();
        }
    }
}
无锁编程

不加锁,三个线程不间断执行,通过一个volatile变量来控制打印次序。

class Foo {

    private volatile int a = 1;

    public Foo() {
        
    }

    public void first(Runnable printFirst) throws InterruptedException {

        while (true) {
            printFirst.run();
            a = 2;
            break;
        }
    }

    public void second(Runnable printSecond) throws InterruptedException {

        while (true) {
            if (a == 2) {
                printSecond.run();
                a = 3;
                break;
            }
        }
    }

    public void third(Runnable printThird) throws InterruptedException {

        while (true) {
            if (a == 3) {
                printThird.run();
                break;
            }    
        }
    }
}

交替打印

题干描述

给你一个类:

class FooBar {
  public void foo() {
    for (int i = 0; i < n; i++) {
      print("foo");
    }
  }

  public void bar() {
    for (int i = 0; i < n; i++) {
      print("bar");
    }
  }
}

两个不同的线程将会共用一个 FooBar 实例:

  • 线程 A 将会调用 foo() 方法,而
  • 线程 B 将会调用 bar() 方法

请设计修改程序,以确保 "foobar" 被输出 n 次。

示例 1:

输入: n = 1
输出: "foobar"
解释: 这里有两个线程被异步启动。其中一个调用 foo() 方法, 另一个调用 bar() 方法,"foobar" 将被输出一次。

示例 2:

输入: n = 2
输出: "foobarfoobar"
解释: "foobar" 将被输出两次。

提示:

  • 1 <= n <= 1000
解题思路

需要在两个循环中使两个线程交替执行打印,要使两个线程交替执行,这两个线程必须得使用同一个对象锁,然后进行线程间协作。同时,要使得两个线程交替执行,还需要一个标志符,表明现在应该那个线程执行打印。

有些使用无锁编程,通过Thread.yield()、Thread.sleep(1)来控制线程的执行次序,这是不靠谱的。因为让出CPU时间后,由操作系统指定那一个线程执行,这个过程比较随机,没法完全保证执行次序。

class FooBar {
    private int n;

    public FooBar(int n) {
        this.n = n;
    }

    private int a = 1;
    
    private Object lock = new Object();
   
    public void foo(Runnable printFoo) throws InterruptedException {

        synchronized (lock) {
            for (int i = 0; i < n; i++) {
                if (a == 2) {
                    lock.wait();
                }
                printFoo.run();
                a = 2;
                lock.notify();
            }
        }
    }

    public void bar(Runnable printBar) throws InterruptedException {

        synchronized (lock) {
            if (a == 1) {
                lock.wait();
            }
            for (int i = 0; i < n; i++) {
                if (a == 1) {
                    lock.wait();
                }
                printBar.run();
                a = 1;
                lock.notify();
            }
        }
    }
}
posted @   cd_along  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示