0

我有一个 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在新的Thread中执行导入操作

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)其执行结束后执行?谢谢!

4

6 回答 6

3

use the .join() method on the thread.

于 2012-08-22T08:31:27.393 回答
2

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

例如,您需要在线程之间共享一个 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
    }


}
于 2012-08-22T09:21:49.940 回答
2

如果使用Swing. _ _
它们包含辅助线程 API

于 2012-08-22T08:38:39.423 回答
0

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

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

这样,您就在 GUI 线程上执行它,但仅在子线程完成其工作时才执行。

于 2012-08-22T11:14:33.493 回答
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
于 2012-08-22T08:38:21.117 回答
0

<Thread>.join() 方法会阻塞当前线程,直到 <Thread> 完成。

于 2012-08-22T08:31:56.987 回答