changeset 8:efaa7ad906b3

Implement Queue in DataGearManager
author Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
date Thu, 28 Dec 2017 18:33:22 +0900
parents 21372a589bd3
children 50a2df302920
files src/main/java/christie/codegear/CodeGearManager.java src/main/java/christie/codegear/StartCodeGear.java src/main/java/christie/datagear/DataGearManager.java src/main/java/christie/datagear/LocalDataGearManager.java src/main/java/christie/test/StartTest.java
diffstat 5 files changed, 35 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/christie/codegear/CodeGearManager.java	Wed Dec 27 18:42:46 2017 +0900
+++ b/src/main/java/christie/codegear/CodeGearManager.java	Thu Dec 28 18:33:22 2017 +0900
@@ -13,10 +13,12 @@
  */
 public class CodeGearManager{
     private ConcurrentHashMap<String, DataGearManager>  dataGearManagers = new ConcurrentHashMap<String, DataGearManager>();
+    private ConcurrentHashMap<String, CodeGearManager> cgms;
     private ThreadPoolExecutor threadPoolExecutor;
 
-    public CodeGearManager(ThreadPoolExecutor exe) {
+    public CodeGearManager(ThreadPoolExecutor exe, ConcurrentHashMap<String, CodeGearManager> cgms) {
         dataGearManagers.put("local", new LocalDataGearManager());
+        this.cgms = cgms;
         threadPoolExecutor = exe;
     }
 
@@ -40,4 +42,8 @@
         cg.setup(this);
     }
 
+    public ConcurrentHashMap<String, CodeGearManager> getCgms() {
+        return cgms;
+    }
+
 }
--- a/src/main/java/christie/codegear/StartCodeGear.java	Wed Dec 27 18:42:46 2017 +0900
+++ b/src/main/java/christie/codegear/StartCodeGear.java	Thu Dec 28 18:33:22 2017 +0900
@@ -6,17 +6,16 @@
 import java.util.concurrent.TimeUnit;
 
 public abstract class StartCodeGear extends CodeGear{
-    static ConcurrentHashMap<String, CodeGearManager> cgms = new ConcurrentHashMap<>();
-    static LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<Runnable>();
-    static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), // initial number of threads
+    ConcurrentHashMap<String, CodeGearManager> cgms = new ConcurrentHashMap<>();
+    LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<Runnable>();
+    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), // initial number of threads
             Runtime.getRuntime().availableProcessors(),
             Integer.MAX_VALUE, // keepAliveTime
             TimeUnit.SECONDS,
             taskQueue);
 
-
-    public static CodeGearManager createCGM(String name){
-        CodeGearManager cgm = new CodeGearManager(threadPoolExecutor);
+    public CodeGearManager createCGM(String name){
+        CodeGearManager cgm = new CodeGearManager(threadPoolExecutor, cgms);
         cgms.put(name, cgm);
         return cgm;
     }
--- a/src/main/java/christie/datagear/DataGearManager.java	Wed Dec 27 18:42:46 2017 +0900
+++ b/src/main/java/christie/datagear/DataGearManager.java	Thu Dec 28 18:33:22 2017 +0900
@@ -6,14 +6,16 @@
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Queue;
 import java.util.TreeMap;
+import java.util.concurrent.LinkedBlockingQueue;
 
 /**
  * Created by e125769 on 12/7/17.
  * PUT/TAKEなどDataGearManagerに対するComandの実行。
  */
 public interface DataGearManager {
-    TreeMap<String, DataGear> dataGears = new TreeMap<String, DataGear>();
+    TreeMap<String, LinkedBlockingQueue<DataGear>> dataGears = new TreeMap<String, LinkedBlockingQueue<DataGear>>();
     HashMap<String, Command> waitList = new HashMap<String, Command>();
     void take(Command cm);
     void put(String key, Object data);
--- a/src/main/java/christie/datagear/LocalDataGearManager.java	Wed Dec 27 18:42:46 2017 +0900
+++ b/src/main/java/christie/datagear/LocalDataGearManager.java	Thu Dec 28 18:33:22 2017 +0900
@@ -2,6 +2,9 @@
 
 import christie.codegear.Command;
 
+import java.util.Queue;
+import java.util.concurrent.LinkedBlockingQueue;
+
 /**
  * Created by e125769 on 12/7/17.
  */
@@ -17,7 +20,14 @@
 
     public void put(String key, Object data) {
         DataGear dg = new DataGear(data, data.getClass());
-        dataGears.put(key, dg);
+        if(dataGears.containsKey(key)){
+            dataGears.get(key).add(dg);
+        } else {
+            LinkedBlockingQueue<DataGear> queue = new LinkedBlockingQueue<DataGear>();
+            queue.add(dg);
+            dataGears.put(key, queue);
+        }
+
         if (waitList.containsKey(key)){
             waitList.get(key).dg.setData(data);
             runCommand(waitList.get(key));
@@ -29,7 +39,10 @@
         switch (cm.type){
             case TAKE:
                     cm.cs.idg.setInputs(cm.key, cm.dg);
-                    dataGears.remove(cm.key);
+                    dataGears.get(cm.key).poll();
+                    if(dataGears.get(cm.key) != null){//要るかな?
+                        dataGears.remove(cm.key);
+                    }
                     break;
             case PEEK:
                     cm.cs.idg.setInputs(cm.key, cm.dg);
--- a/src/main/java/christie/test/StartTest.java	Wed Dec 27 18:42:46 2017 +0900
+++ b/src/main/java/christie/test/StartTest.java	Thu Dec 28 18:33:22 2017 +0900
@@ -7,9 +7,11 @@
 
 public class StartTest extends StartCodeGear{
 
+
     public static void main(String args[]){
-        CodeGearManager cgm = createCGM("first");
-        cgm.setup(new StartTest());
+        StartTest start = new StartTest();
+        CodeGearManager cgm = start.createCGM("first");
+        cgm.setup(start);
     }
 
     @Override
@@ -18,6 +20,7 @@
         cgm.setup(cg);
 
         localDGM.put("hoge", "hogehogehoge");
+        localDGM.put("hoge", "hogehogehoge2");
 
         int array[][] = {{1},{2},{3},{4}};
         localDGM.put("huga", array);