NEW A(MANY)-》SEND MESSAGE(UNIQUE)
NEW B(MANY)-》SET MESSAGE(UNIQUE)
ONLY ONE C(IF A GET MESSAGE A SENT,C remove MESSAGE FROM HASHMAP)
public class AClient2C2 { private final long timeout = 10000; public void test(String taskId) { long count = 0; while (CData2C2.getInstance().isHasTask(taskId) && count < timeout) { try { System.out.println("wait+"+count); Thread.sleep(1000); count += 1000; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(count>=timeout) { System.out.println("超时"); } } public static void main(String[] args){ //B的方法会改变CData2C2的值 CData2C2.getInstance().regesitTaskId("aaaa"); AClient2C2 ac = new AClient2C2(); ac.test("aaaa"); //A的方法获取CData2C2的值会把对象从HASHMAP里删除 CData2C2.getInstance().removeTaskId("aaaa"); } }
import java.util.HashMap; import java.util.Map; public class BService2C2 { }
import java.util.HashMap; import java.util.Map; public class CData2C2 { private static CData2C2 instance; private static Map<String,String> map = new HashMap<String,String>(); private CData2C2() { } public static CData2C2 getInstance(){ if(instance==null){ instance = new CData2C2(); } return instance; } public void regesitTaskId(String taskId){ map.put(taskId, taskId); } public void removeTaskId(String taskId){ map.remove(taskId); } public boolean isHasTask(String taskId){ if(null!=map.get(taskId)){ return Boolean.TRUE; } return Boolean.FALSE; } }
时间: 2024-10-30 13:48:52