生产者消费者以及LRU
来源:互联网 发布:君子去仁 恶乎成名赏析 编辑:程序博客网 时间:2024/06/07 01:17
//生产者消费者//wait()和notify()import java.util.LinkedList; class Producer extends Thread {private final LinkedList<Object> storeHouse; private final int MAX; public Producer(LinkedList<Object> storeHouse,int MAX){this.storeHouse=storeHouse;this.MAX=MAX;} public void run() { while (true) { synchronized (storeHouse) { try { while (storeHouse.size() == MAX) { storeHouse.wait(); } Object newOb = new Object(); if (storeHouse.add(newOb)) { Thread.sleep((long) (Math.random() * 3000)); storeHouse.notify(); } } catch (InterruptedException ie) { System.out.println("producer is interrupted!"); } } } } } class Comsumer extends Thread {private final LinkedList<Object> storeHouse; private final int MAX; public Comsumer(LinkedList<Object> storeHouse,int MAX){this.storeHouse=storeHouse;this.MAX=MAX;} public void run() { while (true) { synchronized (storeHouse) { try { while (storeHouse.size() == 0) { storeHouse.wait(); } storeHouse.removeLast(); Thread.sleep((long) (Math.random() * 3000)); storeHouse.notify(); } catch (InterruptedException ie) { System.out.println("Consumer is interrupted"); } } } } }//生产者消费者//阻塞队列的形式import java.util.concurrent.BlockingQueue;import java.util.concurrent.LinkedBlockingQueue;import java.util.logging.Level;import java.util.logging.Logger;public class ProducerConsumerPattern { public static void main(String args[]){ //Creating shared object BlockingQueue sharedQueue = new LinkedBlockingQueue(); //Creating Producer and Consumer Thread Thread prodThread = new Thread(new Producer(sharedQueue)); Thread consThread = new Thread(new Consumer(sharedQueue)); //Starting producer and Consumer thread prodThread.start(); consThread.start(); } }//Producer Class in javaclass Producer implements Runnable { private final BlockingQueue sharedQueue; public Producer(BlockingQueue sharedQueue) { this.sharedQueue = sharedQueue; } @Override public void run() { for(int i=0; i<10; i++){ try { System.out.println("Produced: " + i); sharedQueue.put(i); } catch (InterruptedException ex) { Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex); } } }}//Consumer Class in Javaclass Consumer implements Runnable{ private final BlockingQueue sharedQueue; public Consumer (BlockingQueue sharedQueue) { this.sharedQueue = sharedQueue; } @Override public void run() { while(true){ try { System.out.println("Consumed: "+ sharedQueue.take()); } catch (InterruptedException ex) { Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex); } } } }Output:Produced: 0Produced: 1Consumed: 0Produced: 2Consumed: 1Produced: 3Consumed: 2Produced: 4Consumed: 3Produced: 5Consumed: 4Produced: 6Consumed: 5Produced: 7Consumed: 6Produced: 8Consumed: 7Produced: 9Consumed: 8Consumed: 9
//LRUpublic class LRUCache { int capacity; HashMap<Integer, Node> map = new HashMap<Integer, Node>(); Node head=null; Node end=null; public LRUCache(int capacity) { this.capacity = capacity; } public int get(int key) { if(map.containsKey(key)){ Node n = map.get(key); remove(n); setHead(n); return n.value; } return -1; } public void remove(Node n){ if(n.pre!=null){ n.pre.next = n.next; }else{ head = n.next; } if(n.next!=null){ n.next.pre = n.pre; }else{ end = n.pre; } } public void setHead(Node n){ n.next = head; n.pre = null; if(head!=null) head.pre = n; head = n; if(end ==null) end = head; } public void set(int key, int value) { if(map.containsKey(key)){ Node old = map.get(key); old.value = value; remove(old); setHead(old); }else{ Node created = new Node(key, value); if(map.size()>=capacity){ map.remove(end.key); remove(end); setHead(created); }else{ setHead(created); } map.put(key, created); } }}
0 0
- 生产者消费者以及LRU
- Boost, C++11,生产者消费者以及Qt生产者消费者例子
- java 生产者和消费者 以及synchronized
- 多线程的生产者以及消费者程序。
- python中的生产者以及消费者实现
- 死锁机制以及生产者与消费者模式
- 生产者/消费者
- 生产者/消费者
- 生产者消费者
- 生产者-消费者
- 生产者消费者
- 生产者 消费者
- 生产者-消费者
- 生产者 消费者
- 生产者、消费者
- 生产者/消费者
- 生产者消费者
- 生产者/消费者
- The 36th ACM/ICPC Asia Regional Dalian Site 1004 The kth great number
- 第三周项目3 求集合并集
- 72. Edit Distance
- 【LeetCode】马三来刷题之Remove Duplicates from Sorted Array
- python中的深拷贝和浅拷贝理解
- 生产者消费者以及LRU
- GCD使用以及多线程开发
- HTTP协议头部与Keep-Alive模式详解
- java编程思想(三)总结
- lightoj-1189-Sum of Factorials【思维】
- android错误:The method makeText(Context, CharSequence, int) in the type
- 用flask开发个人博客(6)—— 为flask程序添加命令行解释器
- Keepalived+Nginx实现高可用(HA)
- 移动端 rem