zoukankan      html  css  js  c++  java
  • java Exchanger 2

    //Listing 6-3. Using an Exchanger to Swap Buffers
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.Exchanger;
    
    public class ExchangerDemo {
        final static Exchanger<DataBuffer> exchanger = new Exchanger<DataBuffer>();
        final static DataBuffer initialEmptyBuffer = new DataBuffer();
        final static DataBuffer initialFullBuffer = new DataBuffer("I");
    
        public static void main(String[] args) {
            class FillingLoop implements Runnable {
                int count = 0;
    
                @Override
                public void run() {
                    DataBuffer currentBuffer = initialEmptyBuffer;
                    try {
                        while (true) {
                            addToBuffer(currentBuffer);
                            if (currentBuffer.isFull()) {
                                System.out
                                        .println("filling thread wants to exchange");
                                currentBuffer = exchanger.exchange(currentBuffer);
                                System.out
                                        .println("filling thread receives exchange");
                            }
                        }
                    } catch (InterruptedException ie) {
                        System.out.println("filling thread interrupted");
                    }
                }
    
                void addToBuffer(DataBuffer buffer) {
                    String item = "NI" + count++;
                    System.out.println("Adding: " + item);
                    buffer.add(item);
                }
            }
            class EmptyingLoop implements Runnable {
                @Override
                public void run() {
                    DataBuffer currentBuffer = initialFullBuffer;
                    try {
                        while (true) {
                            takeFromBuffer(currentBuffer);
                            if (currentBuffer.isEmpty()) {
                                System.out.println("emptying thread wants to "
                                        + "exchange");
                                currentBuffer = exchanger.exchange(currentBuffer);
                                System.out.println("emptying thread receives "
                                        + "exchange");
                            }
                        }
                    } catch (InterruptedException ie) {
                        System.out.println("emptying thread interrupted");
                    }
                }
    
                void takeFromBuffer(DataBuffer buffer) {
                    System.out.println("taking: " + buffer.remove());
                }
            }
            new Thread(new EmptyingLoop()).start();
            new Thread(new FillingLoop()).start();
        }
    }
    
    class DataBuffer {
        private final static int MAXITEMS = 10;
        private final List<String> items = new ArrayList<>();
    
        DataBuffer() {
        }
    
        DataBuffer(String prefix) {
            for (int i = 0; i < MAXITEMS; i++) {
                String item = prefix + i;
                System.out.printf("Adding %s%n", item);
                items.add(item);
            }
        }
    
        synchronized void add(String s) {
            if (!isFull())
                items.add(s);
        }
    
        synchronized boolean isEmpty() {
            return items.size() == 0;
        }
    
        synchronized boolean isFull() {
            return items.size() == MAXITEMS;
        }
    
        synchronized String remove() {
            if (!isEmpty())
                return items.remove(0);
            return null;
        }
    }
  • 相关阅读:
    AcWing 第 12 场周赛
    AtCoder Beginner Contest 170 (D~F题,D筛法,E multiset使用,F Dijkstra算法改进)
    Codeforces Round #650 (Div. 3) F1经典离散化DP
    Codeforces Round #738 (Div. 2) (A~E)
    AtCoder Beginner Contest 214 (D并查集,E反悔贪心,F公共子序列DP)
    c++ 跨平台线程同步对象那些事儿——基于 ace
    博客园排名预测
    关于netty
    全局获取HttpContext
    【设计模式】设计模式学习笔记之(一)——类图、对象之间的关系及设计模式概要
  • 原文地址:https://www.cnblogs.com/rojas/p/5367437.html
Copyright © 2011-2022 走看看