日期:2014-05-20  浏览次数:21071 次

大家能帮忙看下问啥这个程序很慢吗?求优化建议
Java code
public class MyThread extends Thread{
        private CountDownLatch threadsSignal;
        private int hsmapCapacity;
        
        public MyThread(CountDownLatch threadsSignal, int capacity){
            super();
            this.threadsSignal = threadsSignal; 
            this.hsmapCapacity = capacity;
        }
        
        @Override
        public void run(){
            System.out.println(Thread.currentThread().getName() + "Start..."); 
            
            FileReader fr;
            BufferedReader bfr;
            FileWriter fw;
            BufferedWriter bfw;
            HashMap<String,Long> nodes = new HashMap<String,Long>(hsmapCapacity);
            
            String line, rev, s1, s1_rev;
            
            Random rd = new Random();
            int p,len,j;
            long cnt;
            
            try{
                File dir = new File("Maps");
                if(!dir.exists())
                    dir.mkdir();
                
                nodes.clear();
                
                while(blocks.cardinality()<numOfBlocks){
                    
                    p = rd.nextInt(numOfBlocks);
                    
                    while(blocks.get(p)==true)
                        p = rd.nextInt(numOfBlocks);
                    
                    blocks.set(p);
                    
                    fr = new FileReader("Nodes/nodes"+p);
                    bfr = new BufferedReader(fr, bufSize);
                    fw = new FileWriter("Maps/maps"+p);
                    bfw = new BufferedWriter(fw, bufSize);
                    //nodes.clear();
                    
                    while((line = bfr.readLine()) != null){
                        
                        String[] strs = line.split("\t");
                        cnt = new Long(strs[1]);
                        
                        rev = getReverse(getTwin(strs[0]));
                        len = rev.length();
                         
                        long preOriginal = -1, preReplace = -1, Original = -1, Replace = -1;
                        long diff = -1;
                        boolean newOut = true, next = false;
                        
                        
                        for(j = 0; j < strs[0].length() - k + 1; j++){
                            s1 = strs[0].substring(j, k + j);
                            s1_rev = rev.substring(len - j - k, len - j);
                            
                            if(!nodes.containsKey(s1) && !nodes.containsKey(s1_rev)){
                                nodes.put(s1, cnt+j*2);
                                
                                if(!newOut && !next){
                                    bfw.write(preOriginal+"\t"+preReplace);
                                    bfw.newLine();
                                    
                                    newOut = true;
                                }
                                    
                            }
                            else{
                                if(nodes.containsKey(s1)){
                                    Original = cnt+j*2;
                                    Replace = nodes.get(s1);
                                }
                                else if(nodes.containsKey(s1_rev)){
                                    Original = cnt+j*2;
                                    Replace = nodes.get(s1_rev)+1;
                                }
                                
                                if(newOut){
                                    bfw.write(Original+"\t"+Replace);
                                    bfw.newLine();
                                    newOut = false;
                                    next = true;
                                }
                                
                                else if(Original-preOriginal==2){
                                    if(next){
                                        diff = Replace - preReplace;
                                        bfw.write(diff>0?"+":"-");
                                        bfw.newLine();
                                        next = false;
                                    }
                                    else{
                                        if(Replace - preReplace != diff){
                                            bfw.write(preOriginal+"\t"+preReplace);
                                            bfw.newLine();
                                            
                                            bfw.write(Original+"\t"+Replace);
                                            bfw.newLine();
                                            next = true;
                                        }
                                    }
                                }
                                
                                preOriginal = Original;
                                preReplace = Replace;
                            }
                            
                        }
                        
                        if(!newOut && !next){
                            bfw.write(preOriginal+"\t"+preReplace);
                            bfw.newLine();
                        }
                    }
                    
                    nodes.clear();
                    
                    bfw.close();
                    fw.close();
                    bfr.close();
                    fr.close();    
                    
                }
                            
            }catch(Exception E){
                System.out.println("Exception caught!");
                E.printStackTrace();
            }
            
            threadsSignal.countDown();  
            System.out.println(Thread.currentThread().getName() + "End. Remaining" + threadsSignal.getCount() + " threads");  
            
        }
    }
    
    private void BuildMap(int threadNum, int hsmapCapacity) throws Exception{
        CountDownLatch threadSignal = new CountDownLatch(threadNum);
        for(int i=0;i<threadNum;i++){
            Thread t = new MyThread(threadSignal, hsmapCapacity);
            t.start();
        }
        threadSignal.await();
        System.out.println(Thread.currentThread().getName() + "End."); 
    }