diff --git a/pom.xml b/pom.xml
index 401088d..1f8de32 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.snowcattle.game.excutor
game-excutor
- 1.6.7-SNAPSHOT
+ 1.6.8-SNAPSHOT
diff --git a/src/main/java/com/snowcattle/game/excutor/service/UpdateService.java b/src/main/java/com/snowcattle/game/excutor/service/UpdateService.java
index 0126539..cf99a1a 100644
--- a/src/main/java/com/snowcattle/game/excutor/service/UpdateService.java
+++ b/src/main/java/com/snowcattle/game/excutor/service/UpdateService.java
@@ -34,13 +34,6 @@ public class UpdateService {
//记录当前循环的更新接口
private Map updateMap = new ConcurrentHashMap();
-// public UpdateService(DispatchThread dispatchThread, EventBus eventBus, IUpdateExcutor iUpdateExcutor) {
-// this.dispatchThread = dispatchThread;
-// this.eventBus = eventBus;
-// this.iUpdateExcutor = iUpdateExcutor;
-// }
-
-
public UpdateService(DispatchThread dispatchThread, IUpdateExcutor iUpdateExcutor) {
this.dispatchThread = dispatchThread;
this.iUpdateExcutor = iUpdateExcutor;
@@ -56,7 +49,7 @@ public void addReadyCreateEvent(CycleEvent event){
}
CreateEvent createEvent = new CreateEvent(Constants.EventTypeConstans.createEventType, eventParams);
dispatchThread.addCreateEvent(createEvent);
- LockSupport.unpark(dispatchThread);
+ dispatchThread.unpark();
}
public void addReadyFinishEvent(CycleEvent event){
@@ -102,7 +95,16 @@ public void start(){
this.updateMap.clear();
}
+ public void notifyStart(){
+ iUpdateExcutor.start();
+ this.updateMap.clear();
+ }
+
public UpdateService(IUpdateExcutor iUpdateExcutor) {
this.iUpdateExcutor = iUpdateExcutor;
}
+
+ public void notifyRun(){
+ dispatchThread.notifyRun();
+ }
}
diff --git a/src/main/java/com/snowcattle/game/excutor/thread/DispatchThread.java b/src/main/java/com/snowcattle/game/excutor/thread/DispatchThread.java
index bcc84d4..a1c57f2 100644
--- a/src/main/java/com/snowcattle/game/excutor/thread/DispatchThread.java
+++ b/src/main/java/com/snowcattle/game/excutor/thread/DispatchThread.java
@@ -4,6 +4,8 @@
import com.snowcattle.game.excutor.event.IEvent;
import com.snowcattle.game.excutor.utils.Constants;
+import java.util.concurrent.locks.LockSupport;
+
/**
* Created by jiangwenping on 17/1/9.
* ⌚事件分配器
@@ -21,6 +23,10 @@ public void run() {
eventBus.handleEvent();
}
+ public void notifyRun(){
+ eventBus.handleEvent();
+ }
+
public EventBus getEventBus() {
return eventBus;
}
@@ -43,4 +49,11 @@ public void addCreateEvent(IEvent event){
public void addFinishEvent(IEvent event){
getEventBus().addEvent(event);
}
+
+ public void unpark(){
+ LockSupport.unpark(this);
+ }
+ public void park(){
+ LockSupport.park(this);
+ }
}
diff --git a/src/main/java/com/snowcattle/game/excutor/thread/LockSupportDisptachThread.java b/src/main/java/com/snowcattle/game/excutor/thread/LockSupportDisptachThread.java
index c04210c..ff80b7c 100644
--- a/src/main/java/com/snowcattle/game/excutor/thread/LockSupportDisptachThread.java
+++ b/src/main/java/com/snowcattle/game/excutor/thread/LockSupportDisptachThread.java
@@ -30,28 +30,33 @@ public LockSupportDisptachThread(EventBus eventBus, IUpdateExcutor iUpdateExcuto
this.minCycleTime = minCycleTime;
}
+ @Override
public void run() {
while (runningFlag) {
- long time = System.nanoTime();
- int cycleSize = getEventBus().getEventsSize();
- int size = getEventBus().cycle(cycleSize);
- LockSupport.park();
+ singleCycle(true);
+ }
+ }
+ private void singleCycle(boolean sleepFlag){
+ long time = System.nanoTime();
+ int cycleSize = getEventBus().getEventsSize();
+ int size = getEventBus().cycle(cycleSize);
+ if(sleepFlag) {
+ park();
long notifyTime = System.nanoTime();
long diff = (int) (notifyTime - time);
- if(diff < minCycleTime && diff > 0){
+ if (diff < minCycleTime && diff > 0) {
try {
- Thread.currentThread().sleep(cycleSleepTime, (int) (diff%999999));
- } catch (Exception e) {
+ Thread.currentThread().sleep(cycleSleepTime, (int) (diff % 999999));
+ } catch (Throwable e) {
Loggers.utilLogger.error(e.toString(), e);
}
}
-
}
}
-
- public void unpark(){
- LockSupport.unpark(this);
+ @Override
+ public void notifyRun() {
+ singleCycle(false);
}
public EventBus getUpdateServiceEventBus() {
diff --git a/src/main/java/com/snowcattle/game/excutor/thread/LockSupportUpdateThread.java b/src/main/java/com/snowcattle/game/excutor/thread/LockSupportUpdateThread.java
index 434fc69..795ef7b 100644
--- a/src/main/java/com/snowcattle/game/excutor/thread/LockSupportUpdateThread.java
+++ b/src/main/java/com/snowcattle/game/excutor/thread/LockSupportUpdateThread.java
@@ -32,9 +32,7 @@ public void run() {
UpdateEvent event = new UpdateEvent(Constants.EventTypeConstans.updateEventType, params);
event.setUpdateAliveFlag(getiUpdate().isActive());
getEventBus().addEvent(event);
-
- LockSupport.unpark(getDispatchThread());
-
+ getDispatchThread().unpark();
}
}
diff --git a/src/main/java/com/snowcattle/game/excutor/thread/SingleLockSupportUpdateThread.java b/src/main/java/com/snowcattle/game/excutor/thread/SingleLockSupportUpdateThread.java
index 972f2ba..29d7fc0 100644
--- a/src/main/java/com/snowcattle/game/excutor/thread/SingleLockSupportUpdateThread.java
+++ b/src/main/java/com/snowcattle/game/excutor/thread/SingleLockSupportUpdateThread.java
@@ -122,7 +122,7 @@ public void sendFinishList(){
sendFinish(excutorUpdate);
}
finishList.clear();
- LockSupport.unpark(getDispatchThread());
+ getDispatchThread().unpark();
}
diff --git a/src/test/java/com/snowcattle/game/excutor/event/asyncevent/AsyncNotifyEventTest.java b/src/test/java/com/snowcattle/game/excutor/event/asyncevent/AsyncNotifyEventTest.java
new file mode 100644
index 0000000..a60ef75
--- /dev/null
+++ b/src/test/java/com/snowcattle/game/excutor/event/asyncevent/AsyncNotifyEventTest.java
@@ -0,0 +1,74 @@
+package com.snowcattle.game.excutor.event.asyncevent;
+
+import com.snowcattle.game.excutor.event.CycleEvent;
+import com.snowcattle.game.excutor.event.EventBus;
+import com.snowcattle.game.excutor.event.EventParam;
+import com.snowcattle.game.excutor.event.async.IntegerUpdate;
+import com.snowcattle.game.excutor.event.impl.DispatchCreateEventListener;
+import com.snowcattle.game.excutor.event.impl.DispatchFinishEventListener;
+import com.snowcattle.game.excutor.event.impl.DispatchUpdateEventListener;
+import com.snowcattle.game.excutor.pool.UpdateEventExcutorService;
+import com.snowcattle.game.excutor.service.UpdateService;
+import com.snowcattle.game.excutor.thread.LockSupportEventDisptachThread;
+import com.snowcattle.game.excutor.utils.Constants;
+
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.LockSupport;
+
+/**
+ * Created by jwp on 2017/3/28.
+ */
+public class AsyncNotifyEventTest {
+ public static void main(String[] args) throws Exception {
+ testEvent();
+ }
+
+ public static void testEvent() throws Exception {
+ EventBus updateEventBus = new EventBus();
+// int maxSize = 10000;
+// int corePoolSize = 100;
+ int maxSize = 2;
+ int corePoolSize = 2;
+ long keepAliveTime = 60;
+ TimeUnit timeUnit = TimeUnit.SECONDS;
+ UpdateEventExcutorService updateEventExcutorService = new UpdateEventExcutorService(corePoolSize);
+ int cycleSleepTime = 1000 / Constants.cycle.cycleSize;
+ LockSupportEventDisptachThread dispatchThread = new LockSupportEventDisptachThread(updateEventBus, updateEventExcutorService
+ , cycleSleepTime, cycleSleepTime*1000);
+ updateEventExcutorService.setDispatchThread(dispatchThread);
+ UpdateService updateService = new UpdateService(dispatchThread, updateEventExcutorService);
+ updateEventBus.addEventListener(new DispatchCreateEventListener(dispatchThread, updateService));
+ updateEventBus.addEventListener(new DispatchUpdateEventListener(dispatchThread, updateService));
+ updateEventBus.addEventListener(new DispatchFinishEventListener(dispatchThread, updateService));
+
+ updateService.notifyStart();
+// while (true) {
+// Thread.currentThread().sleep(100);
+// LockSupport.unpark(dispatchThread);
+//
+// LockSupport.park(dispatchThread);
+// updateService.notifyRun();
+// break;
+// }
+
+
+ for (long i = 0; i < maxSize; i++) {
+ IntegerUpdate integerUpdate = new IntegerUpdate(i);
+ EventParam param = new EventParam(integerUpdate);
+ CycleEvent cycleEvent = new CycleEvent(Constants.EventTypeConstans.readyCreateEventType, integerUpdate.getId(), param);
+ updateService.addReadyCreateEvent(cycleEvent);
+ }
+
+
+// updateService.shutDown();
+ Timer timer = new Timer();
+ timer.schedule(new NotifyTask(updateService), 0, 10);
+ while (true) {
+ Thread.currentThread().sleep(100);
+ updateService.toString();
+ }
+ }
+}
+
diff --git a/src/test/java/com/snowcattle/game/excutor/event/asyncevent/NotifyTask.java b/src/test/java/com/snowcattle/game/excutor/event/asyncevent/NotifyTask.java
new file mode 100644
index 0000000..dc9000a
--- /dev/null
+++ b/src/test/java/com/snowcattle/game/excutor/event/asyncevent/NotifyTask.java
@@ -0,0 +1,22 @@
+package com.snowcattle.game.excutor.event.asyncevent;
+
+import com.snowcattle.game.excutor.service.UpdateService;
+
+import java.util.TimerTask;
+
+/**
+ * Created by jwp on 2017/3/28.
+ */
+class NotifyTask extends TimerTask {
+
+ private UpdateService updateService;
+
+ public NotifyTask(UpdateService updateService) {
+ this.updateService = updateService;
+ }
+
+ @Override
+ public void run() {
+ updateService.notifyRun();
+ }
+}