Letcode多線程題目

在java中,解決多線程問題常用的類有:CountDownLatch(做減法)、CyclicBarrier(做加法)、Semaphore(信號量) 、 JUC.atomic 包下的類 、CAS 等。
以及ReentrantLock,同步鎖等
常用的關鍵字: volatile

1、多線程保證按序執行。CountDownLatch做減法的例子。
Suppose we have a class:

public class Foo {
  public void first() { print("first"); }
  public void second() { print("second"); }
  public void third() { print("third"); }
}

The same instance of Foo will be passed to three different threads. Thread A will call first(), thread B will call second(), and thread C will call third(). Design a mechanism and modify the program to ensure that second() is executed after first(), and third() is executed after second().

Example 1:

Input: [1,2,3]
Output: "firstsecondthird"
Explanation: There are three threads being fired asynchronously. The input [1,2,3] means thread A calls first(), thread B calls second(), and thread C calls third(). "firstsecondthird" is the correct output.

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/print-in-order
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。

class Foo {

    private CountDownLatch second = new CountDownLatch(1);
    private CountDownLatch third = new CountDownLatch(1);
    public Foo() {
        
    }
    public void first(Runnable printFirst) throws InterruptedException {
        
        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        second.countDown();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        second.await();
        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
        third.countDown();
    }

    public void third(Runnable printThird) throws InterruptedException {
        
        // printThird.run() outputs "third". Do not change or remove this line.
        third.await();
        printThird.run();
    }
}

2、交替打印。(一樣可以用上面的方法,這裏採用其他方法,爲了各樣都涉及下) 信號量舉例。
Suppose you are given the following code:

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");
    }
  }
}

The same instance of FooBar will be passed to two different threads. Thread A will call foo() while thread B will call bar(). Modify the given program to output “foobar” n times.

Example 1:

Input: n = 1
Output: "foobar"
Explanation: There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar(). "foobar" is being output 1 time.

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/print-foobar-alternately
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。

class FooBar {
    private int n;

    public FooBar(int n) {
        this.n = n;
    }
    Semaphore s1=new Semaphore(0);
    Semaphore s2=new Semaphore(1);
    public void foo(Runnable printFoo) throws InterruptedException {
        
        for (int i = 0; i < n; i++) {
            
        	// printFoo.run() outputs "foo". Do not change or remove this line.
        	s2.acquire();
        	printFoo.run();
            s1.release();
        }
    }

    public void bar(Runnable printBar) throws InterruptedException {
        
        for (int i = 0; i < n; i++) {
            
            // printBar.run() outputs "bar". Do not change or remove this line.
        	s1.acquire();
        	printBar.run();
            s2.release();
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章