(三)線程同步工具集_2---控制併發訪問資源的多個副本

控制併發訪問資源的多個副本

在前面的記錄中使用binary semaphore實現了一個例子,這個例子是用來保護一個共享資源;可是semaphore也能夠用來保護能夠同時被多個線程訪問的多個共享資源副本或者臨界區副本;java

在下面的例子中展現使用semaphore去保護多個資源的副本;併發

動手實現

1.建立一個PrintQueuedom

public class PrintQueue {
    // present printer that are free to print a job
    private boolean freePrinters[];
    // Use this lock to protect the access to the freePrinters.
    private Lock lockPrinters;
    private final Semaphore semaphore;

    public PrintQueue() {
        this.semaphore = new Semaphore(3);
        freePrinters = new boolean[3];
        for (int i = 0; i < 3; i++) {
            freePrinters[i]=true;
        }
        lockPrinters=new ReentrantLock();
    }

    public void printJob(){
        try {
            semaphore.acquire();
            int assignedPrinter=getPrinter();
            long duration=(long)(Math.random()*10);
            System.out.printf("%s: PrintQueue: Printing a Job in Printer %d during %d seconds\n",
                    Thread.currentThread().getName(),assignedPrinter,duration);
            TimeUnit.SECONDS.sleep(duration);
            freePrinters[assignedPrinter]=true;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            semaphore.release();
        }
    }
    private int getPrinter(){
        int ret=-1;
        lockPrinters.lock();
        try {
            for (int i = 0; i < freePrinters.length; i++) {
                if (freePrinters[i]) {
                    ret=i;
                    freePrinters[i]=false;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lockPrinters.unlock();
        }
        return ret;

    }
}
2.建立一個job

public class Job implements Runnable {
    private PrintQueue printQueue;

    public Job(PrintQueue printQueue) {
        this.printQueue = printQueue;
    }

    @Override
    public void run() {
        System.out.printf("%s: Going to print a job\n",
                Thread.currentThread().getName());
        printQueue.printJob();
        System.out.printf("%s: The document has been printed\n",
                Thread.currentThread().getName());
    }
}

3.Main

public class Main {
    public static void main(String[] args) {
        PrintQueue printQueue=new PrintQueue();
        Thread[] threads = new Thread[10];
        for (int i = 0; i < 10; i++) {
            threads[i] = new Thread(new Job(printQueue), "Thread_" + i);
        }
        for (int i = 0; i < 10; i++) {
            threads[i].start();
        }
    }
}

要點

在上面的printJob方法中,semaphore每次能夠是三個線程同時訪問,多餘的線程將被阻塞;而進入的這三個線程又造成了競爭,而這裏使用lock進一步去保護共享數據,標記每一次能夠訪問的線程,把索引記錄在freePrinters中;

ide

相關文章
相關標籤/搜索