1

我已经用两台打印机实现了一个问题,两台打印机不能同时打印,例如打印机 A 正在打印而 B 不能,就像它一样简单,我这样做Semaphores如下:

我的Printer.class样子

public class Printer extends Thread {

    Semaphore mutex,multiplex;
    PrinterMachine printerMachine;
    String printer = "";
    public Printer(Semaphore mutex, Semaphore multiplex, PrinterMachine pm) {
        this.multiplex = multiplex;
        this.mutex = mutex;
        printerMachine = pm;
    }
    @Override
    public void run() {
        String printer = "";
        for(;;) {
            try {multiplex.acquire();} catch (InterruptedException e) {}
            try {mutex.acquire();} catch (InterruptedException e) {}
            if(printerMachine.getCanPrintA()) {
                printer = "A";
                printerMachine.setCanPrintA(false); 
            }
            else {
                printer="B";
                printerMachine.setCanPrintB(false); 
            }
            mutex.release();
            try {Thread.sleep(100);} catch (InterruptedException e) {}
            System.out.println(printer);
            if(printer.equals("A")) {
                printerMachine.setCanPrintA(true);
                }
            else {
                printerMachine.setCanPrintB(true);
            }
            try {Thread.sleep(100);} catch (InterruptedException e) {}
            multiplex.release();
        }
    }

}

然后我有一个共享变量的类

class PrinterMachine{
    public volatile Boolean canPrintA = true,canPrintB = true;
.... //Getter and Setter

然后我有我的主要

public static void main(String[] args) {
        Semaphore mutex = /* COMPLETE */ new Semaphore(1);
        Semaphore multiplex = /* COMPLETE */ new Semaphore(2);
        PrinterMachine pm = new PrinterMachine();

        Printer printers[] = new Printer[10];
        for (int i = 0 ; i<printers.length; i++) {
            printers[i] = new Printer(mutex,multiplex,pm);
            printers[i].start();
        }   
        try {
            Thread.sleep(5000);
        }
        catch(InterruptedException ie) {}
        for (int i = 0 ; i<printers.length; i++) {
            printers[i].stop();
        }
    }

它工作正常,但我想知道如何更改我的信号量以monitors代替使用?


编辑

问题?

我有两台打印机,我不能同时打印一个文档(System.out.println()),所以我用信号量做了一个程序来做到这一点,这样我就不能在 A 和 B 打印机上打印同时,现在我正在尝试使用监视器而不是使用信号量来完成它。

4

1 回答 1

0

monitor当您使用良好的旧synchronized代码块/方法时,这是 jvm 为您做的事情。这看起来像这样:

Object mutex = new Object(); // it can be any object, even a class.
                             // However, it's preferred to have a separate class only for this.
for (int i = 0 ; i<printers.length; i++) {
    printers[i] = new Printer(mutex, pm); // object sharing the mutex object
    printers[i].start();
}

在打印机内:

public class Printer extends Thread {
    private final Object mutex;
    // ..

    public Printer (Object mutex, ...) {
        this.mutext = mutex;
    }

    @Override
    public void run() {
        synchronized(mutex) { //all Printers synchronize the same object
            System.out.println("A");
        }
    }

上面的代码块确保代码块中只能有一台打印机synchronized(mutex)。在您的代码中,Semaphore mutex = new Semaphore(1);即两台打印机不能同时存在。这很容易,简单,并且不需要共享内存。

另一方面,Semaphore multiplex = new Semaphore(2);必须是信号量。您可以使用自己的计数器并重新实现Semaphore. 这可能是错误和缓慢的,因为这些问题比看起来更复杂。我建议Semaphore(2)在必要时使用。

免责声明:我不完全理解这个问题。对解决方案进行逆向工程会导致误解。

于 2018-02-26T12:50:43.277 回答