Reputation: 1874
I've a JFrame subclass that manage my UI and call a method of Item Object to import items in my DB:
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
}
The Item object implements Runnable to execute the import operation in a new 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
}
}
How can I modify that code to execute the table.refresh()
only after the Item.importImtes(progressBar)
end its execution? thanks!
Upvotes: 0
Views: 5776
Reputation: 120848
If you want one Thread to wait for another, you can always use a CountDownLatch also. Using one also means that you do not have to wait until some threads finishes completely. If you would make your question better understandable for us, we could help with more details.
You need to share a CountDownLatch for example between the Threads:
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
}
And on the other side:
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
}
}
Upvotes: 2
Reputation: 62439
What you can do is move the table.refresh();
code at the end of the child thread code and use SwingUtilities.InvokeLater
to forward it to the GUI thread event queue:
public void run() {
//...
SwingUtilities.InvokeLater(
new Runnable() {
public void run() {
table.refresh();
}
});
}
That way you are executing it on the GUI thread but only when the child thread finished its work.
Upvotes: 0
Reputation: 54074
Check out SwingWorker and SwingUtilities in case you use Swing
.
They contain helper threading APIs
Upvotes: 2
Reputation: 1326
This is how you should use it:
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");
}
}
OUTPUT:
Main Thread Start
Progress Bar - start
Progress Bar - end
Main Thread End
Upvotes: 0
Reputation: 24124
<Thread>.join() method would block the current thread until <Thread> is complete.
Upvotes: 0