1114. 按序打印
我们提供了一个类:
public class Foo {
public void first() { print("first"); }
public void second() { print("second"); }
public void third() { print("third"); }
}
三个不同的线程将会共用一个 Foo 实例。
线程 A 将会调用 first() 方法
线程 B 将会调用 second() 方法
线程 C 将会调用 third() 方法
请设计修改程序,以确保 second() 方法在 first() 方法之后被执行,third() 方法在 second() 方法之后被执行。
======================Java Semaphore====================
class Foo { private Semaphore sema1 = new Semaphore(1); private Semaphore sema2 = new Semaphore(1); public Foo() { this.sema1 = sema1; this.sema2 = sema2; try { this.sema1.acquire(); this.sema2.acquire(); } catch(InterruptedException e) { e.printStackTrace(); } } public void first(Runnable printFirst) throws InterruptedException { // printFirst.run() outputs "first". Do not change or remove this line. printFirst.run(); this.sema1.release(); } public void second(Runnable printSecond) throws InterruptedException { // printSecond.run() outputs "second". Do not change or remove this line. this.sema1.acquire(); printSecond.run(); this.sema1.release(); this.sema2.release(); } public void third(Runnable printThird) throws InterruptedException { // printThird.run() outputs "third". Do not change or remove this line. this.sema2.acquire(); printThird.run(); this.sema2.release(); } }
=========================Java Atomic=========================
class Foo { private AtomicInteger firstJobDone = new AtomicInteger(0); private AtomicInteger secondJobDone = new AtomicInteger(0); public Foo() {} public void first(Runnable printFirst) throws InterruptedException { // printFirst.run() outputs "first". printFirst.run(); // mark the first job as done, by increasing its count. firstJobDone.incrementAndGet(); } public void second(Runnable printSecond) throws InterruptedException { while (firstJobDone.get() != 1) { // waiting for the first job to be done. } // printSecond.run() outputs "second". printSecond.run(); // mark the second as done, by increasing its count. secondJobDone.incrementAndGet(); } public void third(Runnable printThird) throws InterruptedException { while (secondJobDone.get() != 1) { // waiting for the second job to be done. } // printThird.run() outputs "third". printThird.run(); } }
========================Python Semaphore==========================
class Foo: def __init__(self): self.s1 = threading.Semaphore(1) self.s2 = threading.Semaphore(1) self.s1.acquire() self.s2.acquire() def first(self, printFirst: 'Callable[[], None]') -> None: # printFirst() outputs "first". Do not change or remove this line. printFirst() self.s1.release() def second(self, printSecond: 'Callable[[], None]') -> None: # printSecond() outputs "second". Do not change or remove this line. with self.s1: printSecond() self.s2.release() def third(self, printThird: 'Callable[[], None]') -> None: # printThird() outputs "third". Do not change or remove this line. with self.s2: printThird()
=========================Python Lock==========================
class Foo: def __init__(self): self.l1 = threading.Lock() self.l2 = threading.Lock() self.l1.acquire() self.l2.acquire() def first(self, printFirst: 'Callable[[], None]') -> None: # printFirst() outputs "first". Do not change or remove this line. printFirst() self.l1.release() def second(self, printSecond: 'Callable[[], None]') -> None: # printSecond() outputs "second". Do not change or remove this line. with self.l1: printSecond() self.l2.release() def third(self, printThird: 'Callable[[], None]') -> None: # printThird() outputs "third". Do not change or remove this line. with self.l2: printThird()
=========================Python Event======================
class Foo: def __init__(self): self.e1 = threading.Event() self.e2 = threading.Event() def first(self, printFirst: 'Callable[[], None]') -> None: # printFirst() outputs "first". Do not change or remove this line. printFirst() self.e1.set() def second(self, printSecond: 'Callable[[], None]') -> None: # printSecond() outputs "second". Do not change or remove this line. self.e1.wait() printSecond() self.e2.set() def third(self, printThird: 'Callable[[], None]') -> None: # printThird() outputs "third". Do not change or remove this line. self.e2.wait() printThird()
1195.交替打印字符串
编写一个可以从 1 到 n 输出代表这个数字的字符串的程序,但是:
如果这个数字可以被 3 整除,输出 "fizz"。
如果这个数字可以被 5 整除,输出 "buzz"。
如果这个数字可以同时被 3 和 5 整除,输出 "fizzbuzz"。
例如,当 n = 15,输出: 1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz。
假设有这么一个类:
class FizzBuzz {
public FizzBuzz(int n) { ... } // constructor
public void fizz(printFizz) { ... } // only output "fizz"
public void buzz(printBuzz) { ... } // only output "buzz"
public void fizzbuzz(printFizzBuzz) { ... } // only output "fizzbuzz"
public void number(printNumber) { ... } // only output the numbers
}
请你实现一个有四个线程的多线程版 FizzBuzz, 同一个 FizzBuzz 实例会被如下四个线程使用:
线程A将调用 fizz() 来判断是否能被 3 整除,如果可以,则输出 fizz。
线程B将调用 buzz() 来判断是否能被 5 整除,如果可以,则输出 buzz。
线程C将调用 fizzbuzz() 来判断是否同时能被 3 和 5 整除,如果可以,则输出 fizzbuzz。
线程D将调用 number() 来实现输出既不能被 3 整除也不能被 5 整除的数字。
==========Java CyclicBarrier===================
class FizzBuzz { private int n; private static CyclicBarrier c = new CyclicBarrier(4); public FizzBuzz(int n) { this.n = n; } // printFizz.run() outputs "fizz". public void fizz(Runnable printFizz) throws InterruptedException { for (int i = 1; i <= n; i++) { if (i % 3 == 0 && i % 5 != 0) { printFizz.run(); } try { c.await(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } } // printBuzz.run() outputs "buzz". public void buzz(Runnable printBuzz) throws InterruptedException { for (int i = 1; i <= n; i++) { if (i % 5 == 0 && i % 3 != 0) { printBuzz.run(); } try { c.await(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } } // printFizzBuzz.run() outputs "fizzbuzz". public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException { for (int i = 1; i <= n; i++) { if (i % 3 == 0 && i % 5 == 0) { printFizzBuzz.run(); } try { c.await(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } } // printNumber.accept(x) outputs "x", where x is an integer. public void number(IntConsumer printNumber) throws InterruptedException { for (int i = 1; i <= n; i++) { if (i % 3 != 0 && i % 5 != 0) { printNumber.accept(i); } try { c.await(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } } }