Concurrent 四

十、Semaphore

Semaphore是用来控制对某些资源的访问的线程的数量的,就是一个集体资源的守门人

它一共有以下几种方法:

1public void acquire() throws InterruptedException

获得一张门票,获得之后门派就会少一张,程序可以访问资源,如果没有获得呢,当前线程就会中断,还会抛出一个异常。

2public void acquireUninterruptibly()

和上面一样,但是不中断,不抛异常,它会等有人访问完毕了再进去访问。

3public boolean tryAcquire()

尝试去买门派,买到了就返回true,而且门派会少一张;如果没有买到呢,就会返回false

4public boolean tryAcquire(long timeout, TimeUnit unit)

在有限的时间里排队买票,如果买到了就进门,买不到就自杀。

5public void release()

守门人再多给别人一个机会。

6public void acquire(int permits) throws InterruptedException

一个人买多张票,买不到就自杀。

7public void acquireUninterruptibly(int permits)

一个人不停的买多张门票,买不到继续等机会。

8public boolean tryAcquire(int permits)

尝试买多个门票,买到了就返回true,而且门票会少几张;如果没有买到呢,就会返回false

9public boolean tryAcquire(int permits, long timeout, TimeUnit unit)

尝试在有限的时间内买多个门票,买到了就返回true,而且门票会少几张;如果没有买到呢,就会返回false

10public void release(int permits)

守门人再多给别人几个机会。

11public int availablePermits()

看还剩下多少张门票。

12public boolean isFair()

是否按照先进先出的规则进行资源的访问。

13public int getQueueLength()

取得队列的长度。

 

例子:

/*

 * Created on 2004-8-25

 */

package test1;

 

import java.util.concurrent.Semaphore;

 

/**  

 * @author <a href="mailto:Azure_2003@126.com">Azure</a>

 *

 */

public class Myte {

    public static int number = 1;

    public final static int MAX = 5;

    Semaphore semaphore = new Semaphore(MAX, true);

 

    public static void main(String[] args) {

       Myte myte = new Myte();

 

       Runnable worker = new Thread(myte.new Worker());

       Runnable worker2 = new Thread(myte.new Worker());

 

       System.out.println("all the permits number :" + MAX);

       try {

           worker.run();

           myte.getItem();

 

           worker2.run();

           myte.getItem();

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

 

       System.out.println("game over!");

 

    }

 

    public void getItem() throws InterruptedException {

       System.out.println("remain permits : " + semaphore.availablePermits());

       System.out.println("release permits : " + 2);

       semaphore.release(2);

       System.out.println("remain permits : " + semaphore.availablePermits());

       System.out.println(

           "semaphore.getQueueLength()=" + semaphore.getQueueLength());

    }

 

    class Worker implements Runnable {

       public void run() {

           for (int i = 1; i < 3; i++) {

              System.out.println("run thread " + i);

              semaphore.acquireUninterruptibly();

              try {

                  semaphore.acquire();

              } catch (InterruptedException e) {

                  e.printStackTrace();

              }

              System.out.println("run over thread " + i);

           };

 

           number++;

       }

 

    }

}

 

打印结果:

all the permits number :5

run thread 1

run over thread 1

run thread 2

run over thread 2

remain permits : 1

release permits : 2

remain permits : 3

semaphore.getQueueLength()=0

run thread 1

run over thread 1

run thread 2

 

程序正在堵死中。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值