Show
Ignore:
Timestamp:
10/02/07 12:49:26 (1 year ago)
Author:
max
Message:
 
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/multithread/src/planet/generic/commonapi/MultiThreadedNetworkImpl.java

    r20 r28  
    6161     * Containts all nodes to remove from the network. 
    6262     */ 
    63     private Stack toRemove; 
     63    private Stack<NodeHandle> toRemove; 
    6464    /** 
    6565     * NodeFactory implementation which permits build new nodes. 
     
    8080 
    8181    private final int PROCESSORS; 
     82    private final Thread[] threads; 
     83    private final ProcessGroup[] processGroups; 
     84//    private final Boolean[] continues; 
    8285    /** 
    8386     * Initialize the network with no nodes and wihtout simulation steps. 
     
    8588    public MultiThreadedNetworkImpl() { 
    8689        PROCESSORS = Properties.simulatorProcessors; 
     90        threads = new Thread[PROCESSORS]; 
     91        processGroups = new ProcessGroup[PROCESSORS]; 
     92        for (int i = 0; i < processGroups.length; i++) { 
     93                        processGroups[i] = new ProcessGroup(); 
     94                } 
     95//      continues = new Boolean[PROCESSORS]; 
    8796        nodes = new HashMap<NodeHandle, Node>();    //to contain all nodes 
    8897        toRemove = new Stack<NodeHandle>(); 
     
    553562     
    554563         
    555         Thread[] threads = new Thread[PROCESSORS]; 
     564         
    556565        Node[] nodes = new Node[this.nodes.size()]; 
    557566        nodes = this.nodes.values().toArray(nodes); 
    558         Boolean[] continues = new Boolean[PROCESSORS]; 
    559         //init continues 
    560         for (int i = 0; i < continues.length; i++) { 
    561                         continues[i] = false; 
    562         } 
     567         
     568 
    563569 
    564570         
     
    568574                         
    569575                        int from = i*groupSize; 
     576                         
    570577                        //if it is the last thread it gets all remaining nodes, i.e. 7 nodes and two processors groupA {0..2} and groupB {3..6} 
    571                         int to = i==threads.length-1 ? nodes.length : from+groupSize;  
    572                         ProcessGroup group = new ProcessGroup(nodes,from,to,continues,i); 
    573                         Thread thread = new Thread(group); 
    574                         threads[i] = thread; 
     578                        int to = i==threads.length-1 ? nodes.length : from+groupSize; 
     579                         
     580                        //ProcessGroup group = new ProcessGroup(nodes,from,to,continues,i); 
     581                        processGroups[i].setFrom(from); 
     582                        processGroups[i].setTo(to); 
     583                        processGroups[i].setNodes(nodes); 
     584                        threads[i] = new Thread(processGroups[i]); 
     585                         
     586                          
    575587                } 
    576588        //start all threads 
     
    595607          aNode = (Node)it.next(); 
    596608          // always process the node 
    597           if (!aNode.isAlive()) { //the node has failed or leaved?? 
     609          if (!aNode.isAlive()) { //the node has failed or left?? 
    598610              toRemove.add(aNode.getLocalHandle()); 
    599611          } 
    600612        } 
    601613        boolean toContinue = false; 
    602         for (Boolean b : continues) { 
    603                         toContinue |=b; 
     614        for (int i = 0; i < processGroups.length; i++) { 
     615                        toContinue |= processGroups[i].isToContinue(); 
    604616                } 
    605617        return toContinue; 
     
    836848        private Node[] nodes = null; 
    837849        private int from,to; 
    838         private Boolean[] continues = null; 
    839         private int nr; 
     850//      private int nr; 
     851        private boolean toContinue; 
    840852        /** 
    841853         *  
     
    846858         * @param nr the positon where to set in  <b>conitues</b> <code>true</code> or <code>false</code> 
    847859         */ 
    848         public ProcessGroup(Node[] nodes, int from, int to, 
    849                                 Boolean[] continues, int nr) { 
    850                         super(); 
    851                         this.nodes = nodes; 
    852                         this.from = from; 
    853                         this.to = to; 
    854                         this.continues = continues; 
    855                         this.nr = nr; 
    856                 } 
     860         
    857861 
    858862                public void run() { 
    859863                         
    860                         boolean toContinue = false; 
     864                        toContinue = false; 
    861865                for (int i = from; i < to; i++) { 
    862866                                Node n = nodes[i]; 
    863867                                toContinue |= n.process(totalSteps); 
    864868                        } 
    865                 continues[nr]=toContinue; 
     869                 
    866870                 
    867871        } 
     872                public void setNodes(Node[] nodes) { 
     873                        this.nodes = nodes; 
     874                } 
     875                public void setFrom(int from) { 
     876                        this.from = from; 
     877                } 
     878                public void setTo(int to) { 
     879                        this.to = to; 
     880                } 
     881                public boolean isToContinue() { 
     882                        return toContinue; 
     883                } 
    868884    } 
    869885}