Skip to content

Commit

Permalink
修正updatethread太快没有阻塞
Browse files Browse the repository at this point in the history
  • Loading branch information
jwpttcg66 committed Feb 27, 2017
1 parent 73cd80c commit d637ba8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.snowcattle.game.excutor.thread.listener.LockSupportUpdateFutureListener;
import com.snowcattle.game.excutor.update.IUpdate;
import com.snowcattle.game.excutor.utils.Constants;
import com.snowcattle.game.excutor.utils.Loggers;

/**
* Created by jiangwenping on 17/1/11.
Expand All @@ -26,6 +27,9 @@ public DispatchUpdateEventListener(LockSupportDisptachThread dispatchThread, Upd


public void fireEvent(IEvent event) {
// if(Loggers.utilLogger.isDebugEnabled()){
// Loggers.utilLogger.debug("处理update");
// }
super.fireEvent(event);

//提交执行线程
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.snowcattle.game.excutor.utils.Constants;
import com.snowcattle.game.excutor.utils.Loggers;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand All @@ -15,6 +16,7 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.LockSupport;
import java.util.logging.Logger;

/**
* Created by jwp on 2017/2/23.
Expand Down Expand Up @@ -57,6 +59,8 @@ public void run() {
excutorUpdate.update();
updateSize++;
finishList.add(excutorUpdate);
}else{
break;
}
} catch (Exception e) {
Loggers.errorLogger.error(e.toString(), e);
Expand All @@ -70,6 +74,13 @@ public void run() {

}
cleanFetch();

//这里会运行的太快,需要阻塞
try {
fetchUpdates.take();
} catch (InterruptedException e) {
Loggers.errorLogger.error(e.toString(), e);
}
}

}
Expand Down Expand Up @@ -102,9 +113,12 @@ public void sendFinish(IUpdate excutorUpdate) {
}

public void sendFinishList(){

//事件总线增加更新完成通知
for(IUpdate excutorUpdate : finishList){
// System.out.println(excutorUpdate.getId() + "存活" + excutorUpdate.isActive());
// if(Loggers.utilLogger.isDebugEnabled()) {
// Loggers.utilLogger.debug(excutorUpdate.getId() + "发送存活" + excutorUpdate.isActive());
// }
sendFinish(excutorUpdate);
}
finishList.clear();
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
log4j.rootLogger=info,stdout,error
log4j.logger.gameExcutorUtil=info,gameExcutorUtil
log4j.logger.gameExcutorError=info,gameExcutorError
log4j.logger.gameExcutorUtil=debug,gameExcutorUtil
log4j.logger.gameExcutorError=debug,gameExcutorError

#--------------------stdout--------------------------------
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public IntegerUpdate(long id) {
@Override
public void update() {
incrId++;
if (incrId == 2) {
if (incrId == 2000) {
setActive(false);
}
if (Loggers.utilLogger.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public static void testEvent() throws Exception {
EventBus updateEventBus = new EventBus();
// int maxSize = 10000;
// int corePoolSize = 100;
int maxSize = 1;
int corePoolSize = 1;
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*1000000);
, cycleSleepTime, cycleSleepTime*1000);
updateEventExcutorService.setDispatchThread(dispatchThread);
UpdateService updateService = new UpdateService(dispatchThread, updateEventExcutorService);
updateEventBus.addEventListener(new DispatchCreateEventListener(dispatchThread, updateService));
Expand Down
12 changes: 11 additions & 1 deletion src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
log4j.rootLogger=info,stdout,error
log4j.logger.gameExcutorUtil=debug,gameExcutorUtil
log4j.logger.gameExcutorError=debug,gameExcutorError

#--------------------stdout--------------------------------
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
Expand All @@ -22,9 +23,18 @@ log4j.appender.gameExcutorUtil=org.apache.log4j.DailyRollingFileAppender
log4j.appender.gameExcutorUtil.encoding=UTF-8
log4j.appender.gameExcutorUtil.append=true
log4j.appender.gameExcutorUtil.datePattern='.'yyyy-MM-dd
log4j.appender.gameExcutorUtil.File=logs/gameExcutorUtil.log
log4j.appender.gameExcutorUtil.File=logs/util.log
log4j.appender.gameExcutorUtil.layout=org.apache.log4j.PatternLayout
log4j.appender.gameExcutorUtil.layout.ConversionPattern=%d{HH:mm:ss} - %m%n

#--------------------gameExcutorError--------------------------------
log4j.appender.gameExcutorError=org.apache.log4j.DailyRollingFileAppender
log4j.appender.gameExcutorError.encoding=UTF-8
log4j.appender.gameExcutorError.append=true
log4j.appender.gameExcutorError.datePattern='.'yyyy-MM-dd
log4j.appender.gameExcutorError.File=logs/gameExcutorError.log
log4j.appender.gameExcutorError.layout=org.apache.log4j.PatternLayout
log4j.appender.gameExcutorError.layout.ConversionPattern=%d{HH:mm:ss} - %m%n



0 comments on commit d637ba8

Please sign in to comment.