zoukankan      html  css  js  c++  java
  • [Java]LeetCode1114. 按序打印 | Print in Order

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址://www.cnblogs.com/strengthen/p/11289226.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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.
    Example 2:

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

    Note:

    We do not know how the threads will be scheduled in the operating system, even though the numbers in the input seems to imply the ordering. The input format you see is mainly to ensure our tests' comprehensiveness.


    我们提供了一个类:

    public class Foo {
      public void one() { print("one"); }
      public void two() { print("two"); }
      public void three() { print("three"); }
    }
    三个不同的线程将会共用一个 Foo 实例。

    线程 A 将会调用 one() 方法
    线程 B 将会调用 two() 方法
    线程 C 将会调用 three() 方法
    请设计修改程序,以确保 two() 方法在 one() 方法之后被执行,three() 方法在 two() 方法之后被执行。

    示例 1:

    输入: [1,2,3]
    输出: "onetwothree"
    解释:
    有三个线程会被异步启动。
    输入 [1,2,3] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 two() 方法,线程 C 将会调用 three() 方法。
    正确的输出是 "onetwothree"。
    示例 2:

    输入: [1,3,2]
    输出: "onetwothree"
    解释:
    输入 [1,3,2] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 three() 方法,线程 C 将会调用 two() 方法。
    正确的输出是 "onetwothree"。

    注意:

    尽管输入中的数字似乎暗示了顺序,但是我们并不保证线程在操作系统中的调度顺序。

    你看到的输入格式主要是为了确保测试的全面性。


    10ms

     1 import java.util.concurrent.Semaphore;
     2 
     3 class Foo {
     4     Semaphore A;
     5     Semaphore B;
     6     Semaphore C;
     7     
     8     public Foo() {
     9         A = new Semaphore(1);
    10         B = new Semaphore(0);
    11         C = new Semaphore(0);
    12     }
    13 
    14     public void first(Runnable printFirst) throws InterruptedException {
    15         A.acquire();
    16         // printFirst.run() outputs "first". Do not change or remove this line.
    17         printFirst.run();
    18         B.release();
    19     }
    20 
    21     public void second(Runnable printSecond) throws InterruptedException {
    22         B.acquire();
    23         // printSecond.run() outputs "second". Do not change or remove this line.
    24         printSecond.run();
    25         C.release();
    26     }
    27 
    28     public void third(Runnable printThird) throws InterruptedException {
    29         C.acquire();
    30         // printThird.run() outputs "third". Do not change or remove this line.
    31         printThird.run();
    32     }
    33 }

    13ms

     1 class Foo {
     2 
     3    private boolean firstFinished;
     4     private boolean secondFinished;
     5     private Object lock = new Object();
     6 
     7     public Foo() {
     8         
     9     }
    10 
    11     public void first(Runnable printFirst) throws InterruptedException {
    12         
    13         synchronized (lock) {
    14             // printFirst.run() outputs "first". Do not change or remove this line.
    15             printFirst.run();
    16             firstFinished = true;
    17             lock.notifyAll(); 
    18         }
    19     }
    20 
    21     public void second(Runnable printSecond) throws InterruptedException {
    22         
    23         synchronized (lock) {
    24             while (!firstFinished) {
    25                 lock.wait();
    26             }
    27         
    28             // printSecond.run() outputs "second". Do not change or remove this line.
    29             printSecond.run();
    30             secondFinished = true;
    31             lock.notifyAll();
    32         }
    33     }
    34 
    35     public void third(Runnable printThird) throws InterruptedException {
    36         
    37         synchronized (lock) {
    38            while (!secondFinished) {
    39                 lock.wait();
    40             }
    41 
    42             // printThird.run() outputs "third". Do not change or remove this line.
    43             printThird.run();
    44         } 
    45     }
    46 }
  • 相关阅读:
    SQL CHECKOUT
    Adobe CS4 " Licensing for this product has expired " FIX!!!
    sizeof()用法汇总
    Command
    EXP_FULL_DATABASE,IMP_FULL_DATABASE,DBA,CONNECT,RESOURCE
    inet_addr函数处理IP地址需要注意的问题 (转)
    Oracle:外键关联导致数据无法删除
    三范式
    Wireshark界面上展开数据帧
    我的HTML学习记录(三)
  • 原文地址:https://www.cnblogs.com/strengthen/p/11289226.html
Copyright © 2011-2022 走看看