java semaphore

java基于semaphore方式实现对资源的并发访问,也是多线程同步的一种方式,它一般是用于实现对多个资源副本的并发访问控制,而synchronized,lock等同步方式不同则用于针对一个资源的并发访问控制,也就是说资源只允许一个线程同时访问,而semaphore则可以控制某几个线程同时访问资源。
当然semaphore它也有一个变种,即二进制信号量,它的作用实际上和synchronized和lock就差不多了,二进制信号量也是限制资源只允许一个线程同时访问。
具体的内容可以参考下面的例子。

package com.basic.thread;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class BasicSemaphore {
    public static void main(String args[]) {
        PrintQueueSem pq = new PrintQueueSem();
        Thread thread[] = new Thread[10];

        for (int i=0; i<10; i++) {
            thread[i] = new Thread(new Job(pq), "Thread" + i);
        }

        for (int i=0; i<10; i++) {
            thread[i].start();
        }
    }
}

class PrintQueue {
    private final Semaphore semaphore;

    public PrintQueue() {
        semaphore = new Semaphore(1);
    }

    public void printJob(Object doc) {
        try {
            semaphore.acquire();
            long duration = (long) (Math.random()*10);
            System.out.printf("%s: PrintQueue: Printing a Job during %d seconds\n",Thread.currentThread().getName(),duration);
            Thread.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            semaphore.release();
        }
    }
}

class PrintQueueSem {
    private boolean freePrinters[];
    private Lock lockPrinter;
    private final Semaphore semaphore;

    public PrintQueueSem() {
        semaphore = new Semaphore(3);

        freePrinters = new boolean[3];
        for (int i = 0; i < 3; i++) {
            freePrinters[i] = true;
        }
        lockPrinter = new ReentrantLock();
    }

    public void printJob(Object obj) {
        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);
            Thread.sleep(duration);
            freePrinters[assignedPrinter] = true;
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            semaphore.release();
        }
    }

    private int getPrinter() {
        int ret = -1;
        try {
            lockPrinter.lock();
            for (int i=0; i<freePrinters.length; i++) {
                if (freePrinters[i]) {
                    ret = i;
                    freePrinters[i]=false;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lockPrinter.unlock();
        }

        return ret;
    }
}

class Job implements Runnable {
    /**
    private PrintQueue printQueue;
    public Job(PrintQueueSem pq) {
        this.printQueue = pq;
    }
    */

    private PrintQueueSem printQueue;
    public Job(PrintQueueSem pq) {
        this.printQueue = pq;
    }

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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值