码迷,mamicode.com
首页 > 其他好文 > 详细

thread safe queue --cruise

时间:2018-10-24 10:43:25      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:class   integer   int   oid   put   over   set   static   string   

Implement a Queue with pop and push operations using concurrency coding.

 

 

package com.company;
import java.util.*;
import java.util.concurrent.locks.*;

class BQueue<T> {

     Condition isFullCondition;
     Condition isEmptyCondition;
     Lock lock;
int limit;
int cur = 0;
Queue<T> q = new LinkedList<>();

     public BQueue() {
         this(Integer.MAX_VALUE);
     }

     public BQueue(int limit) {
         this.limit = limit;
         lock = new ReentrantLock();
         isFullCondition = lock.newCondition();
         isEmptyCondition = lock.newCondition();
     }

     public void put (T t) {
         lock.lock();
         try {
             while (q.size() == limit) {
                 try {
                     isFullCondition.await();
                 } catch (InterruptedException ex) {}
             }
             q.add(t);
             isEmptyCondition.signalAll();
         } finally {
             lock.unlock();
         }
     }

     public T get() {
         T t = null;
         lock.lock();
         try {
             while (q.size() == 0) {
                 try {
                     isEmptyCondition.await();
                 } catch (InterruptedException ex) {}
             }
             t = q.poll();
             isFullCondition.signalAll();
         } finally {
             lock.unlock();
         }
         return t;
     }
}


class Producer extends Thread{

    private BQueue data;

    public Producer(BQueue data, String name) {
        this.data = data;
        this.setName(name);
    }

    @Override
    public void run() {
        for (int i=0; i<100 && data.cur < data.limit; i++) {
            data.put(data.cur);
            data.cur++;
            System.out.println(currentThread().getName() + " " + data.cur);
        }
    }
}


class Consumer extends Thread {

    private BQueue data;

    public Consumer(BQueue data, String name) {
        this.data = data;
        this.setName(name);
    }

    @Override
    public void run() {
        for (int i=0; i<data.limit; i++) {
            System.out.println(currentThread().getName()+ " " + i + "   " + data.get());
        }
    }
}




public class Test {

    public static void main (String args[]) {
        BQueue<Integer> data = new BQueue(10);
        new Producer(data, "put---1").start();
        new Producer(data, "put---2").start();
        new Consumer(data, "get---1").start();
        new Consumer(data, "get---2").start();

    }
}

  

thread safe queue --cruise

标签:class   integer   int   oid   put   over   set   static   string   

原文地址:https://www.cnblogs.com/apanda009/p/9841064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!