class A {
    public static  void setEdit(Integer value){
    }
}class B {
    //执行数据库操作
    public static  void query() {
   }     
}
class C {
     for(int i = 0 ; i < 100 ; i ++) {
      A.setEdit();
}
      B.query();
}
要求在C里面一边执行A.setEdit(),一边执行B.query。B.query是执行数据库查询,只需要执行一遍,A.setEdit()需要多次。当B.query执行完,A.setEdit()也执行完毕。

解决方案 »

  1.   

    为什么要一边执行A.setEdit(),一边执行B.query呢?
    等A.setEdit()执行完啦,再执行B.query不行呢
      

  2.   

    你的意思是不是要让setEdit()并发执行,之后执行一次查询呢?是的话,这样做:把单个setEdit()写为一个线程在C里这样做:
    第一个循环里,开启所有线程。
    第二个循环里让所有的线程join()。等待所有的edit操作结束
    然后执行query
      

  3.   

    我猜楼主的意思,在 query 里面进行的操作是一个耗时比较长的数据库查询(包括遍历查询结果),而在 setEdit 里面是显示查询的过程(进度)。请问楼主是这样吗?如果是的话,我再帮你想办法  ;)
      

  4.   

    回maquan('ma:kju) 对,就是这样~
      

  5.   

    回maquan('ma:kju) 谢谢,最好有代码~
      

  6.   

    package test;public class TestProgress {
        public static void main(String[] args) {
            MyProgress prog = new MyProgress();
            new Thread(prog).start();
            DB.query();
            prog.stop();
        }
    }class MyProgress implements Runnable {
        private boolean bStop = false;
        private int count = 0;
        public void run() {
            while(!bStop) {
                UI.setEdit(new Integer(count++));
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) { }
            }
        }
        public synchronized void stop() {
            bStop = true;
        }
    }class UI {
        public static void setEdit(Integer value) {
            System.out.println(value);
        }
    }class DB {
        public static void query() {
            System.out.println("before query.");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("after query.");
        }
    }
      

  7.   

    不过,如果你的 UI 是 Swing 的话,就没有 System.out.print 这么简单了,建议你去查一个叫做 SwingWorker 的东西。