《敏捷软件开发》第12章 AcitveObject模式构建多线程

看了我好久才看懂的代码。
 
ActiveObjectEngine.java
  1. package active;   
  2.   
  3. /**  
  4.  *  
  5.  * @author Administrator  
  6.  */  
  7. import java.util.LinkedList;   
  8.   
  9. public class ActiveObjectEngine {   
  10.   
  11.     LinkedList itsCommands = new LinkedList();   
  12.   
  13.     public void addCommand(Command c) {   
  14.         itsCommands.add(c);   
  15.     }   
  16.   
  17.     public void run() {   
  18.         while (!itsCommands.isEmpty()) {   
  19.             Command c = (Command) itsCommands.getFirst();   
  20.             itsCommands.removeFirst();   
  21.             try {   
  22.                 c.execute();   
  23.             } catch (Exception e) {   
  24.             }   
  25.         }   
  26.     }   
  27. }  

 

Command.java
  1. package active;   
  2.   
  3. /**  
  4.  *  
  5.  * @author Administrator  
  6.  */  
  7. public interface Command {   
  8.     public void execute() throws Exception;   
  9. }  

 

DelayedTyper.java
  1. package active;   
  2.   
  3. /**  
  4.  *  
  5.  * @author Administrator  
  6.  */  
  7. public class DelayedTyper implements Command{   
  8.     private long itsDelay;   
  9.     private char itsChar;   
  10.     private static ActiveObjectEngine engine=new ActiveObjectEngine();   
  11.     private static boolean stop=false;   
  12.        
  13.     public static void main(String args[]) throws Exception{   
  14.         engine.addCommand(new DelayedTyper(100,'1'));   
  15.         engine.addCommand(new DelayedTyper(300,'3'));   
  16.         engine.addCommand(new DelayedTyper(500,'5'));   
  17.         engine.addCommand(new DelayedTyper(700,'7'));   
  18.            
  19.         Command stopCommand=new Command(){   
  20.             public void execute(){   
  21.                 stop=true;   
  22.             }   
  23.         };   
  24.            
  25.         engine.addCommand(new SleepCommand(20000,engine,stopCommand));   
  26.         engine.run();   
  27.     }   
  28.        
  29.     public DelayedTyper(long delay,char c){   
  30.         itsDelay=delay;   
  31.         itsChar=c;   
  32.     }   
  33.        
  34.     public void execute() throws Exception{   
  35.         System.out.print(itsChar);   
  36.         if(!stop){   
  37.             delayAndRepeat();   
  38.         }   
  39.     }   
  40.        
  41.     public void delayAndRepeat() throws CloneNotSupportedException{   
  42.         engine.addCommand(new SleepCommand(itsDelay,engine,this));   
  43.     }    
  44. }  

 

SleepCommand.java
  1. package active;   
  2.   
  3. /**  
  4.  *  
  5.  * @author Administrator  
  6.  */  
  7. public class SleepCommand implements Command{   
  8.     private Command wakeupCommand=null;   
  9.     private ActiveObjectEngine engine=null;   
  10.     private long sleepTime=0;   
  11.     private long startTime=0;   
  12.     private boolean started=false;   
  13.        
  14.     public SleepCommand(long milliseconds,ActiveObjectEngine e,Command wakeupCommand){   
  15.         sleepTime=milliseconds;   
  16.         engine=e;   
  17.         this.wakeupCommand=wakeupCommand;   
  18.     }   
  19.        
  20.     public void execute() throws Exception{   
  21.         long currentTime=System.currentTimeMillis();   
  22.         if(!started){   
  23.             started=true;   
  24.             startTime=currentTime;   
  25.             engine.addCommand(this);   
  26.         }else if((currentTime-startTime)
  27.             engine.addCommand(this);   
  28.         }else{   
  29.             engine.addCommand(wakeupCommand);   
  30.         }   
  31.     }   
  32. }  

 

SleepCommandTest.java
  1. package active;   
  2.   
  3. import org.junit.After;   
  4. import org.junit.AfterClass;   
  5. import org.junit.Before;   
  6. import org.junit.BeforeClass;   
  7. import org.junit.Test;   
  8. import static org.junit.Assert.*;   
  9.   
  10. /**  
  11.  *  
  12.  * @author Administrator  
  13.  */  
  14. public class SleepCommandTest {   
  15.        
  16.     public SleepCommandTest() {   
  17.     }   
  18.   
  19.     @BeforeClass  
  20.     public static void setUpClass() throws Exception {   
  21.     }   
  22.   
  23.     @AfterClass  
  24.     public static void tearDownClass() throws Exception {   
  25.     }   
  26.   
  27.     @Before  
  28.     public void setUp() throws Exception {   
  29.     }   
  30.   
  31.     @After  
  32.     public void tearDown() throws Exception {   
  33.     }   
  34.     public boolean commandExecuted=false;   
  35.     @Test  
  36.     public void execute() throws Exception {   
  37.         Command wakeup=new Command(){   
  38.             public void execute(){commandExecuted=true;}   
  39.         };   
  40.            
  41.         ActiveObjectEngine e=new ActiveObjectEngine();   
  42.         SleepCommand c=new SleepCommand(1000,e,wakeup);   
  43.         e.addCommand(c);   
  44.         long start=System.currentTimeMillis();   
  45.         e.run();   
  46.         long stop=System.currentTimeMillis();   
  47.         long sleepTime=(stop-start);   
  48.         System.out.println(sleepTime);   
  49.         assert(sleepTime>1000);   
  50.         assert(sleepTime>1000);   
  51.         assert(commandExecuted);   
  52.     } /* Test of execute method, of class SleepCommand. */  
  53.        
  54. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值