zoukankan      html  css  js  c++  java
  • JDK 5.0 Concurrency Utilities 并发处理(4)Semaphore 信号量

    Semaphore 信号量





      1package com.vinko.test.concurrent;
      2
      3import java.util.Calendar;
      4import java.util.concurrent.Semaphore;
      5import java.util.concurrent.locks.Condition;
      6import java.util.concurrent.locks.Lock;
      7import java.util.concurrent.locks.ReentrantLock;
      8
      9public class TestSemaphore {
     10
     11    private ReentrantLock lock = null;
     12    private Condition condition = null;
     13    private Semaphore semaphore = null;
     14    
     15    public TestSemaphore() {
     16        lock = new ReentrantLock();
     17        condition = lock.newCondition();
     18        semaphore = new Semaphore(2true);
     19    }

     20    
     21    /**
     22     * @param args
     23     */

     24    public static void main(String[] args) {
     25
     26        TestSemaphore tester = new TestSemaphore();
     27        
     28        tester.test();
     29    }

     30    
     31    public Lock getLock() {
     32        return lock;
     33    }

     34    
     35    public Condition getCondition() {
     36        return condition;
     37    }

     38    
     39    public Semaphore getSemaphore() {
     40        return semaphore;
     41    }

     42    
     43    public void test() {
     44        try {
     45            /*
     46            semaphore.acquire();
     47            System.out.println("get semaphore");
     48            
     49            semaphore.acquire();
     50            System.out.println("get semaphore");
     51
     52            semaphore.release();
     53
     54            semaphore.acquire();
     55            System.out.println("get semaphore");
     56            
     57            semaphore.acquire();
     58            System.out.println("get semaphore");
     59            */

     60            
     61            new Thread(new TestThread(this)).start();
     62            new Thread(new TestThread(this)).start();
     63            new Thread(new TestThread(this)).start();
     64            new Thread(new TestThread(this)).start();
     65            
     66            Thread.sleep(3000);
     67            
     68            lock.lock();
     69            condition.signal();
     70//            condition.signal();
     71//            condition.signalAll();
     72            lock.unlock();
     73            
     74        }
     catch (InterruptedException e) {
     75            e.printStackTrace();
     76        }

     77    }

     78
     79}

     80
     81class TestThread implements Runnable {
     82    
     83    private TestSemaphore tester = null;
     84    
     85    public TestThread(TestSemaphore tester) {
     86        this.tester = tester;
     87    }

     88    
     89    public void run() {
     90        
     91        Calendar now = Calendar.getInstance();
     92        
     93        System.out.println(now.getTime() + " " + Thread.currentThread() + " started.");
     94
     95        while (true{
     96            try {
     97                tester.getLock().lock();
     98                tester.getCondition().await();
     99                tester.getLock().unlock();
    100                
    101                Calendar now1 = Calendar.getInstance();
    102                System.out.println(now1.getTime() + " " + Thread.currentThread() + " got signal.");
    103                
    104                tester.getSemaphore().acquire();
    105                
    106                Calendar now2 = Calendar.getInstance();
    107                System.out.println(now2.getTime() + " " + Thread.currentThread() + " got semaphore.");
    108                
    109//                tester.getSemaphore().release();
    110                
    111            }
     catch (InterruptedException e) {
    112                e.printStackTrace();
    113            }

    114        }

    115    }

    116}
  • 相关阅读:
    vue 底层面试题
    js第二阶段的面试题
    vue新一轮的面试题
    vue3面试题
    day_33:后端day04Django框架中的视图和请求、响应
    day_37:后端day08Django框架前后端不分离模式实现项目管理系统(增删差改)
    day_36:后端day07Django框架中的ORM数据库操作二
    【漏洞复现】ThinkAdmin v5和v6 未授权列目录任意文件读取(CVE202025540)
    【超详细】安全测试===sqlmap使用心得(零)
    【最新】绕过Outlook 拦截钓鱼链接方式
  • 原文地址:https://www.cnblogs.com/kylindai/p/322670.html
Copyright © 2011-2022 走看看