Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangwenping committed Apr 10, 2017
2 parents 9bf9e5c + e60c76e commit 7f035ef
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.snowcattle.game.excutor</groupId>
<artifactId>game-excutor</artifactId>
<version>1.6.7-SNAPSHOT</version>
<version>1.6.8-SNAPSHOT</version>


<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ public class UpdateService <ID extends Serializable> {
//记录当前循环的更新接口
private Map<ID, IUpdate> updateMap = new ConcurrentHashMap<ID, IUpdate>();

// 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;
Expand All @@ -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){
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* ⌚事件分配器
Expand All @@ -21,6 +23,10 @@ public void run() {
eventBus.handleEvent();
}

public void notifyRun(){
eventBus.handleEvent();
}

public EventBus getEventBus() {
return eventBus;
}
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void sendFinishList(){
sendFinish(excutorUpdate);
}
finishList.clear();
LockSupport.unpark(getDispatchThread());
getDispatchThread().unpark();

}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<IntegerUpdate> param = new EventParam<IntegerUpdate>(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();
}
}
}

Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 7f035ef

Please sign in to comment.