等到执行下一个方法之前执行线程结束

时间:2012-08-22 08:30:03

标签: java multithreading

我有一个JFrame子类来管理我的UI并调用Item Object的方法来导入我的数据库中的项目:

public TestGUI() throws IOException
{

    initComponents();

    Item.importItems(progressBar); // static method that execute a new thread to import item witouth freeze my UI (I update a progressBar in it)

    table.refresh(); //Metodh that I need to execute only after "importItems()" methods is completed

}

Item对象实现Runnable以在新线程中执行导入操作

public Item implements Runnable
{

    private JProgressBar progressBar;

    public void importItems (JProgressBar progressBar) throws IOException //Metodo per importare articoli da un file esterno. Usa i Thread.
    {

        this.progressBar = progressBar;

        Thread importThread = new Thread (new RefreshTable(),"Importer Thread");

        importThread.start();

    }

    void run ()
    {

        // I execute here all the DB operation that i need. I also update the progress bar             here

    }


}

如何在table.refresh()结束执行后修改该代码才能执行Item.importImtes(progressBar)?谢谢!

6 个答案:

答案 0 :(得分:3)

在线程上使用.join()方法。

答案 1 :(得分:2)

如果您使用Swing,请查看SwingWorkerSwingUtilities 它们包含辅助线程API

答案 2 :(得分:2)

如果您希望一个线程等待另一个线程,您也可以始终使用 CountDownLatch 。使用一个也意味着您不必等到某些线程完全完成。如果您 让我们的问题更易于理解,我们可以提供更多详细信息。

您需要在Threads:

之间共享一个CountDownLatch
public TestGUI() throws IOException{
      initComponents();
      private final CountDownLatch latch = new CountDownLatch(1);
      Item.importItems(progressBar, latch); // static method that execute a new thread to import item witouth freeze my UI (I update a progressBar in it)
      latch.await();
      table.refresh(); //Metodh that I need to execute only after "importItems()" methods is completed
}

另一方面:

public Item implements Runnable {
    private JProgressBar progressBar;
    public void importItems (JProgressBar progressBar, CountDownLatch latch) throws IOException { //Metodo per importare articoli da un file esterno. Usa i Thread.

        this.progressBar = progressBar;

        Thread importThread = new Thread (new RefreshTable(),"Importer Thread");

        importThread.start();

    }

    void run () {

        // I execute here all the DB operation that i need. I also update the progress bar here
        //After everything finishes:
        latch.countDown(); // you want this to be executed in a finally block of the try catch
    }


}

答案 3 :(得分:0)

< Thread> .join()方法将阻止当前线程,直到< Thread>已经完成了。

答案 4 :(得分:0)

这是你应该如何使用它:

class ProgressBar extends Thread{
    public void run(){
        System.out.println("Progress Bar - start");
        try {
            Thread.currentThread().sleep(2000);
        } catch (InterruptedException e) {}
        System.out.println("Progress Bar - end");
    }
}
public class ThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("Main Thread Start");
        Thread t1 = new Thread(new ProgressBar());
        t1.start();
        t1.join();
        System.out.println("Main Thread End");
    }
}

输出:

Main Thread Start
Progress Bar - start
Progress Bar - end
Main Thread End

答案 5 :(得分:0)

您可以做的是将table.refresh();代码移动到子线程代码的末尾,并使用SwingUtilities.InvokeLater将其转发到GUI线程事件队列:

 public void run() {
      //...
      SwingUtilities.InvokeLater(
            new Runnable() {
                 public void run() {
                     table.refresh();
                 }
            });         
 }

这样你就可以在GUI线程上执行它,但只有当子线程完成它的工作时才会执行它。