从db读取数据并同时显示进度指示器

时间:2014-03-13 18:14:58

标签: java multithreading progress-bar

在从数据库中读取数据时,我需要帮助显示进度指示器。我做的一个例子是在新线程中间中断并继续运行主线程。当主线程执行到结束时,则继续执行另一个线程。这有什么缺失?

private void loadingData() throws InterruptedException{

    prgIndLogin.setVisible(true);
    prgIndLogin.setProgress(-1);

    Thread newThrd = new Thread(new Runnable() {
        @Override
        public void run() {


            try {
                KorisnikJpaController korCont = new KorisnikJpaController(Persistence.createEntityManagerFactory("LawOfficeV2PU"));
                EntityManager em = korCont.getEntityManager();

                logedUser = (Korisnik) em.createNamedQuery("Korisnik.findByUsernameAndPassword")
                                        .setParameter("usernamekor", tbxUsername.getText())
                                        .setParameter("passwordkor", passField.getText())
                                        .getSingleResult();

                em.close();
                status=true;
            } catch(Exception e) {
                status = false;
                e.printStackTrace();
            } finally {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        prgIndLogin.setVisible(false);
                    }
                });
            }
        }
    });
    newThrd.start();

}

0 个答案:

没有答案