关于启用 多 线程 检索。

[color=red]我在一个 while循环的 多线程查询 程序 中[/color]
出现了 个问题,
ServiceThread st = new ServiceThread(url, "a web service thread");
st.start();

这个线程 没 把 自己run方法 完全 执行完,就 去 执行 这个while 循环 后面的内容去了。

关键代码如下:在一个while循环中每循环一次 取得 一个url地址,然后 根据 这个url地址进行 查询,我每一次查询 都启用一个线程。
while (it.hasNext()){
linkedListCell=null;
foo = (Element) it.next();

 String url= foo.elementText("url") ; 
 url= url+"SearchForYou rvices archCell";


 ServiceThread st = new ServiceThread(url, "a web service thread");
 st.start();      

}
[color=red] 1.是不是可以 这个后面 来一句 判断 ,前面的线程 是否 结束了 ,然后 再 执行 下面的 啊?怎么 判断呢?[/color]

 //获取结束时间
   long end=System.currentTimeMillis(); 
   //打印一下        
   system.out.println(end);

  double intervalTime=(end-start)*0.001;

  //本类 执行完之后转向 page所指向的 action类  
  String target = "Page";
  return mapping.findForward(target);

另外我的 ServiceThread部分代码如下:
public class ServiceThread extends Thread{
private String ip; //每一个线程对象启动时要访问的ip
private String searchContent;
public ServiceThread(String ip, String str,String searchContent){
super(str);
this.ip = ip;
this.searchContent=searchContent;//传过来 的查询内容,即查询关键字。
}

  public void run(){
      //根据ip创建相应的web Service并通过它进行远程方法调用
     String  url= ip+"SearchForYou/services/SearchCell";


     //构建 URL
     URL aUrl = null;
     try {
        aUrl = new URL(url);
     } catch (MalformedURLException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
     } 
     System.out.println("已经生成了URL");

     //下面的代码 新建 web service 服务,并通过 它 进行 远程方法 调用。
     Service  service = new Service();
     Call call = null;

     System.out.println("创建了service");
     try {
        call = (Call) service.createCall();

      } catch (ServiceException e1) {
            // TODO Auto-generated catch block
        e1.printStackTrace();
      }

     System.out.println("(Call) service.createCall()");

     call.setTargetEndpointAddress( aUrl );


     call.setOperationName("execute");


     call.setEncodingStyle("UTF-8");


     String linkedListStr =null;
     Integer timeout= new Integer("0");


     try{

           linkedListStr = (String) call.invoke(new Object [] { searchContent });

           call.setTimeout(timeout);

         }catch(Exception e){
             System.out.println(url+"连接超时,这台机子程序没有配置完全,或者网络连接有问题");
             linkedListStr=null;
         }
        LinkedList linkedListCell=null;
        if(linkedListStr!=null )linkedListCell=(LinkedList)decodeBean(linkedListStr);  // 如果对方机器 传递过来的结果集--xml串不为空,则 对其进行decode处理。
        System.out.println("linkedListCell=(LinkedList)decodeBean(linkedListStr)");
        System.out.println(linkedListCell.size());
         //把从 每一个服务器内 获取的 记录集加到内存 中的静态链表 中
        if(linkedListCell!=null )GlobalUtil.resultLinkedList.addAll(linkedListCell);  
        System.out.println("总链表中个数"+GlobalUtil.resultLinkedList.size());

  }

[quote]1.是不是可以 这个后面 来一句 判断 ,前面的线程 是否 结束了 ,然后 再 执行 下面的 啊?怎么 判断呢? [/quote]你的意思是并发执行多个线程, 然后等待所有线程执行完毕在回到主线程吗?如果是这样你可以使用java.util.concurrent这个JDK引进的线程包, 很强大啊!下面是上述情况的一个示意性实现:
[code="java"]public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(10);
for (int i = 1; i < 11; i++) {
final int j = i;
pool.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(j * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("---------------child thread ---------->" + j);
            }
        });
    }

    /** ********start 这段代码来自ExecutorService的javaDoc, 这里等待所有线程执行完, 你可以注掉这块代码看看输出的效果******** */
    pool.shutdown();// 启动一次顺序关闭,执行以前提交的任务,但不接受新任务
    try {
        // 等待所有的任务完成, 如果在预定的时间内没有完成, 试图停止所有正在执行的活动任务
        if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
            pool.shutdownNow();
            if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
                System.err.println("Pool did not terminate");
            }
        }
    } catch (InterruptedException ie) {
        pool.shutdownNow();
        Thread.currentThread().interrupt();
    }
    /** *******end ********** */

    System.out.println("---------------main thread ---------->");
    for (int i = 0; i < 10; i++) {
        System.out.println("---------------other print ---------->" + i);
    }
}[/code]

1.是不是可以 这个后面 来一句 判断 ,前面的线程 是否 结束了 ,然后 再 执行 下面的 啊?怎么 判断呢?

楼主,JAVA有提供的类,有可以做到当前的活动线程,线程结束,自动调用,那就用线程池这个,肯定可以满足楼主你的需求的,网上面这方面的资料很多的.