Wait until end of thread execution before executing the next method

后端 未结 6 2130
慢半拍i
慢半拍i 2021-01-28 19:28

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();         


        
6条回答
  •  不要未来只要你来
    2021-01-28 20:10

    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
    

提交回复
热议问题