Concurrent - Semaphore - tryAcquire

原创转载请注明出处:http://agilestyle.iteye.com/blog/2343117

 


 

tryAcquire()

无参方法tryAcquire()的作用是尝试获取1个permits,如果获取不到则返回false,此方法通常与if语句结合使用,具有无阻塞的特点。

Service.java

package org.fool.java.concurrent.semaphore.tryacquire;

import java.util.concurrent.Semaphore;

public class Service {
    private Semaphore semaphore = new Semaphore(1);

    public void testMethod() {
        if(semaphore.tryAcquire()) {
            System.out.println(Thread.currentThread().getName() + " invoked...");

            for (int i = 0; i < Integer.MAX_VALUE; i++) {
                String str = new String();
                Math.random();
            }

            semaphore.release();
        } else {
            System.out.println(Thread.currentThread().getName() + " not invoked...");
        }
    }
}

ThreadA.java

package org.fool.java.concurrent.semaphore.tryacquire;

public class ThreadA implements Runnable {
    private Service service;

    public ThreadA(Service service) {
        this.service = service;
    }

    @Override
    public void run() {
        service.testMethod();
    }
}

TheadB.java

package org.fool.java.concurrent.semaphore.tryacquire;

public class ThreadB implements Runnable {
    private Service service;

    public ThreadB(Service service) {
        this.service = service;
    }

    @Override
    public void run() {
        service.testMethod();
    }
}

TryAcquireTest.java

package org.fool.java.concurrent.semaphore.tryacquire;

public class TryAcquireTest {
    public static void main(String[] args) {
        Service service = new Service();

        Thread a = new Thread(new ThreadA(service));

        a.setName("A");
        a.start();

        Thread b = new Thread(new ThreadB(service));

        b.setName("B");
        b.start();
    }
}

Run


 

tryAcquire(int permits)

有参方法tryAcquire(int permits)的作用是尝试获取x个permits,如果获取不到则返回false。

Service.java

package org.fool.java.concurrent.semaphore.tryacquire_1;

import java.util.concurrent.Semaphore;

public class Service {
    private Semaphore semaphore = new Semaphore(3);

    public void testMethod() {
        if(semaphore.tryAcquire(3)) {
            System.out.println(Thread.currentThread().getName() + " invoked...");

            for (int i = 0; i < Integer.MAX_VALUE; i++) {
                String str = new String();
                Math.random();
            }

            semaphore.release(3);
        } else {
            System.out.println(Thread.currentThread().getName() + " not invoked...");
        }
    }
}

ThreadA.java

package org.fool.java.concurrent.semaphore.tryacquire_1;

public class ThreadA implements Runnable {
    private Service service;

    public ThreadA(Service service) {
        this.service = service;
    }

    @Override
    public void run() {
        service.testMethod();
    }
}

ThreadB.java

package org.fool.java.concurrent.semaphore.tryacquire_1;

public class ThreadB implements Runnable {
    private Service service;

    public ThreadB(Service service) {
        this.service = service;
    }

    @Override
    public void run() {
        service.testMethod();
    }
}

ThreadC.java

package org.fool.java.concurrent.semaphore.tryacquire_1;

public class ThreadC implements Runnable {
    private Service service;

    public ThreadC(Service service) {
        this.service = service;
    }

    @Override
    public void run() {
        service.testMethod();
    }
}

TryAcquireTest2.java

package org.fool.java.concurrent.semaphore.tryacquire_1;

public class TryAcquireTest2 {
    public static void main(String[] args) {
        Service service = new Service();

        Thread a = new Thread(new ThreadA(service));

        a.setName("A");
        a.start();

        Thread b = new Thread(new ThreadB(service));

        b.setName("B");
        b.start();

        Thread c = new Thread(new ThreadC(service));

        c.setName("C");
        c.start();
    }
}

Run 


 

tryAcquire(long timeout, TimeUnit unit)

有参方法tryAcquire(long timeout, TimeUnit unit)的作用是在指定的时间内尝试获取1个permits,如果获取不到则返回false。 

修改Service.java

package org.fool.java.concurrent.semaphore.tryacquire_2;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class Service {
    private Semaphore semaphore = new Semaphore(1);

    public void testMethod() {
        try {
            if(semaphore.tryAcquire(3, TimeUnit.SECONDS)) {
                System.out.println(Thread.currentThread().getName() + " invoked...");

                for (int i = 0; i < Integer.MAX_VALUE / 10; i++) {
                    new String();
                    Math.random();
                }

                semaphore.release();
            } else {
                System.out.println(Thread.currentThread().getName() + " not invoked...");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

TryAcquireTest3.java

package org.fool.java.concurrent.semaphore.tryacquire_2;

public class TryAcquireTest3 {
    public static void main(String[] args) {
        Service service = new Service();

        Thread a = new Thread(new ThreadA(service));

        a.setName("A");
        a.start();

        Thread b = new Thread(new ThreadB(service));

        b.setName("B");
        b.start();

        Thread c = new Thread(new ThreadC(service));

        c.setName("C");
        c.start();
    }
}

Run


 

修改Service.java,注释掉for循环中的两行 

for (int i = 0; i < Integer.MAX_VALUE / 10; i++) {
	//new String();
	//Math.random();
}

再Run


 

tryAcquire(int permits, long timeout, TimeUnit unit)

有参方法tryAcquire(int permits, long timeout, TimeUnit unit)的作用是在指定的时间内尝试获取x个permits,如果获取不到则返回false。 

修改Service.java

package org.fool.java.concurrent.semaphore.tryacquire_3;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class Service {
    private Semaphore semaphore = new Semaphore(3);

    public void testMethod() {
        try {
            if(semaphore.tryAcquire(3, 3, TimeUnit.SECONDS)) {
                System.out.println(Thread.currentThread().getName() + " invoked...");

                for (int i = 0; i < Integer.MAX_VALUE / 10; i++) {
                    new String();
                    Math.random();
                }

                semaphore.release(3);
            } else {
                System.out.println(Thread.currentThread().getName() + " not invoked...");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

TryAcquireTest4.java

package org.fool.java.concurrent.semaphore.tryacquire_3;

public class TryAcquireTest4 {
    public static void main(String[] args) {
        Service service = new Service();

        Thread a = new Thread(new ThreadA(service));

        a.setName("A");
        a.start();

        Thread b = new Thread(new ThreadB(service));

        b.setName("B");
        b.start();

        Thread c = new Thread(new ThreadC(service));

        c.setName("C");
        c.start();
    }
}

Run


 

修改Service.java,注释掉for循环中的两行 

for (int i = 0; i < Integer.MAX_VALUE / 10; i++) {
	//new String();
	//Math.random();
}
再Run


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值