首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

快看这个线程()帮忙修改

2012-01-10 
快看这个线程(高手进)帮忙修改文件1:Test.javapackagetestimportjava.util.ArrayListimportjava.util.Li

快看这个线程(高手进)帮忙修改
文件1   :Test.javapackage   test;

import   java.util.ArrayList;
import   java.util.List;


/**
*   我想做的是:每次循环里面运算一批数据,为了加快对这批数据的处理,在每一个循环中启用多线程来运算。
*   我想每个循环结束后再继续下一次。
*   但运行结果不是这样,它是每一个循环开始启用线程后(这些线程可能还没有运行),立即进入下一次循环了。
*/
public   class   Test  
{
      public   static   void   main(String   args[])   throws   Exception
{
                                int   threadNumber   =   100;                     //线程数
                                int   cycle   =   10;                                     //循环次数
                                int   number   =   5000;                               //每个循环要处理的数量
                               
                    MyThread   myThread[]   =     new   MyThread[threadNumber];
                                long   begin   =   System.currentTimeMillis();
                               
                               
                                for(int   i=0;i <cycle;i++){
                                                System.out.println( "第 "+i+ "次循环开始 ");
                                                List   list   =   new   ArrayList();
                                                for(int   j=0;j <number;j++){
                                                                list.add(i+ "= "+j);
                                                }
                                               
        int   numberPerThread   =   number     /   threadNumber;     //每个线程要处理的数据

                                               


          for(int   k=0;k <threadNumber;k++)
          {
                    int   startList   =   k   *   numberPerThread;
                    int   endList     =   (k+1)   *   numberPerThread;
                                                               
                    myThread[k]   =   new   MyThread();
                    myThread[k].setList(list.subList(startList,endList));
                    myThread[k].start();
          }
          System.out.println( "第 "+i+ "次循环结束 ");
      }

                                long   end   =   System.currentTimeMillis();
                                System.out.print( "time= "   +   (end-begin));
                }
}

文件二:MyThread.javapackage   test;

import   java.util.List;

public   class   MyThread     extends   Thread{
                List   list   =   null;              
               
                public   void   setList(List   list)   {
                                this.list   =   list;
                }
               
                public   void   run()   {
                                for(int   i=0;i <list.size();i++){
                                                System.out.println(getName()+ "           i= "+i);
                                }
                }
}


[解决办法]
主线程

while(count!=threadcount){

}
[解决办法]
主线程

while(count!=threadcount){
wait();
}

子线程结束时
主线程.count++
notifyall()

热点排行