(Multi-threaded) leetcode1195. The simplest solution alternately print a string variable to get

Can write a digital output representative of the program is from 1 to n strings, but:

If this number can be divisible by 3, the output of "fizz".
If this number can be divisible by 5, the output of "buzz".
If this number can be simultaneously divisible by 3 and 5, the output "fizzbuzz".
For example, when n = 15, output: 1, 2, fizz, 4 , buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz.

Suppose there is such a class:

FizzBuzz {class
  public FizzBuzz (n-int) {...} // constructor
  public void Fizz (printFizz) {...} // Output only "Fizz"
  public void Buzz (printBuzz) {...} // Output only "Buzz"
  public void FizzBuzz (printFizzBuzz) {...} // only the Output "FizzBuzz"
  public void Number the (printNumber) {...} // only the Output at the a Numbers
}
Please realize you have four threads of a multithreaded version FizzBuzz, FizzBuzz instance is the same as four threads:

A thread calls Fizz () to determine whether or not divisible by 3, if so, output fizz.
Buzz thread B calls () can be divisible by 5 to determine whether, and if so, the output buzz.
The calling thread FizzBuzz C () to determine whether simultaneously divisible by 3 and 5, if so, output fizzbuzz.
The thread D call number () is used to achieve either not be divisible by 3 5 divisible numbers.

 

Meaning of the questions: four methods were four kinds of digital printing, but Yao An execution order.

Thinking: digital number represents the current need to print (execution), the other thread determines not their own are not executable. +1 to the current digital execution.

import java.util.concurrent.Semaphore;

class FizzBuzz {
    private int n;
    private AtomicInteger number = new AtomicInteger(1);//当前应该打印的数字

    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){
                while(number.get()!=i)Thread.yield();
                printFizz.run();
                number.set(number.get()+1);
            }
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        for (int i = 0; i <= n; i++) {
            if(i % 3 != 0 && i % 5 == 0){
                while(number.get()!=i)Thread.yield();
                printBuzz.run();
                number.set(number.get()+1);
            }
        }
    }

    // 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){
                while(number.get()!=i)Thread.yield();
                printFizzBuzz.run();
                number.set(number.get()+1);
            }
        }
    }

    // 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) {
                while(number.get()!=i)Thread.yield();
                printNumber.accept(i);
                number.set(number.get()+1);
            }
        }
    }
}

 

Published 616 original articles · won praise 10000 + · views 1.53 million +

Guess you like

Origin blog.csdn.net/hebtu666/article/details/104739987