Java 窗口8上的进度条冻结

Java 窗口8上的进度条冻结,java,swing,swingworker,Java,Swing,Swingworker,嗨,我正在使用一个框架,我正在上面加载一个对话框。singleton对话框包含一个进度条。 我创造了自己的秋千工人。我正在控制swing Worker的构造方法,其功能类似于Doin Background方法。 我试图从外部类调用hidedialog。对话框有时会卡住。我该如何解决它。它在win7上工作正常,但在win8上失败 public class LoadingProgressDialogSingleton extends com.manu.scpoweb.common.ds.clie

嗨,我正在使用一个框架,我正在上面加载一个对话框。singleton对话框包含一个进度条。 我创造了自己的秋千工人。我正在控制swing Worker的构造方法,其功能类似于Doin Background方法。 我试图从外部类调用hidedialog。对话框有时会卡住。我该如何解决它。它在win7上工作正常,但在win8上失败

  public class LoadingProgressDialogSingleton extends com.manu.scpoweb.common.ds.client.swing.dialog.Dialog {
    protected static LoadingProgressDialogSingleton oneAndOnlyProgressDialog = null;
    JProgressBar progressBar = null;
    DFULoadStatusBean dfuLoadStatusBean = null;
    double progress;
    int progressComplete = 0;
    BusinessObjectUserContext uc = null;
    CopyProgressBarWorker copyProgressBarWorker = null;
    ProgressBarWorker progressBarWorker = null;
    public static synchronized LoadingProgressDialogSingleton getProgressDialog(IApplicationContext iApplicationContext, ClientDataManager dataManager){
        if (oneAndOnlyProgressDialog == null){
            oneAndOnlyProgressDialog = new LoadingProgressDialogSingleton(iApplicationContext.getParentFrame(),
                    iApplicationContext.getResource(APMConstants.RESOURCE_BUNDLE, "ProgressBar.Loading"), Boolean.TRUE);

            oneAndOnlyProgressDialog.progressBar = new JProgressBar(0, 100);
            oneAndOnlyProgressDialog.progressBar.setValue(0);
            oneAndOnlyProgressDialog.progressBar.setPreferredSize(new Dimension(175, 20));
            oneAndOnlyProgressDialog.progressBar.setStringPainted(true);
            oneAndOnlyProgressDialog.progressBar.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

            JLabel label = new JLabel(iApplicationContext.getResource(APMConstants.RESOURCE_BUNDLE, "ProgressBar.Reloading"));

            JPanel center_panel = new JPanel();
            center_panel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1;
            gbc.insets = new Insets(2, 6, 2, 6);
            center_panel.add(label, gbc);
            gbc.gridy = 1;
            center_panel.add(oneAndOnlyProgressDialog.progressBar, gbc);
            gbc.gridy = 2;
            center_panel.add(Box.createVerticalGlue(), gbc);

            oneAndOnlyProgressDialog.getContentPane().add(center_panel, BorderLayout.CENTER);
            oneAndOnlyProgressDialog.setLocationRelativeTo(null); // center on screen
            oneAndOnlyProgressDialog.setPreferredSize(new Dimension(350, 107));
            oneAndOnlyProgressDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

            oneAndOnlyProgressDialog.uc = new BusinessObjectUserContext(iApplicationContext.getUser().getUserName(),
                               iApplicationContext.getUserLocale());
            oneAndOnlyProgressDialog.uc.setAttributes(dataManager.getUserContext().getUserPreferences());
        }
        return oneAndOnlyProgressDialog;

    }
    public void showDialog(java.util.List<AttributeUUID> promoOfferKeysOnly){

        final Thread showDialogThread = new Thread(new Runnable() {
                public void run() {
                    toFront();
                    show();
                }
            });
        showDialogThread.start();

        if (promoOfferKeysOnly != null && promoOfferKeysOnly.size() > 0 ) {
           progressBarWorker = new ProgressBarWorker();
           BusinessObjectUserContext uc = new BusinessObjectUserContext(oneAndOnlyProgressDialog.uc.getUserName(),
                   oneAndOnlyProgressDialog.uc.getLocale());
           uc.setAttributes(oneAndOnlyProgressDialog.uc.getUserPreferences());
           progressBarWorker.setBusinessObjectUserContext(uc);
           progressBarWorker.setDataKey(promoOfferKeysOnly.get(0));
           progressBarWorker.start();
        }
    }

    public static synchronized void hideDialog(){
        if (oneAndOnlyProgressDialog != null){
            ProgressBarWorker worker = oneAndOnlyProgressDialog.progressBarWorker;
            CopyProgressBarWorker copyWorker = oneAndOnlyProgressDialog.copyProgressBarWorker;
            oneAndOnlyProgressDialog.dispose();

            if(oneAndOnlyProgressDialog.isShowing())
            oneAndOnlyProgressDialog.setVisible(false);
            oneAndOnlyProgressDialog = null;
            if (worker!=null){
                worker.setProgressComplete();
            }
            if (copyWorker!=null){
                copyWorker.setCopyCompleted();
            }
        }
    }
    protected LoadingProgressDialogSingleton(Frame iParent, String iTitle, boolean iModal) {
        super(iParent, iTitle, iModal);
    }
    class ProgressBarWorker extends SwingWorker {
        BusinessObjectUserContext uc = null;
        AttributeUUID dataKey = null;

        public AttributeUUID getDataKey() {
            return dataKey;
        }

        public void setDataKey(AttributeUUID dataKey) {
            this.dataKey = dataKey;
        }
        public void setProgressComplete(){
            progressComplete = 100;
        }
        public Object construct() {
            progressComplete = 0;
            EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
            if (queue instanceof ManuEventQueue) {
                try {
                    JAASUtilities.runAs(((ManuEventQueue) Toolkit.getDefaultToolkit().getSystemEventQueue()).getSubject(),
                            new PrivilegedExceptionAction() {
                                public Object run() throws Exception {
                                    long totalDFUsCnt = 0;
                                    long basePriceLoadedCnt = 0;
                                    long baseCostLoadedCnt = 0;
                                    long udasLoadedCnt = 0;
                                    long histLoadedCnt = 0;
                                    long fcstLoadedCnt = 0;
                                    progressBar.setValue(5);
                                    //Initialize progress property.
                                    while (progressComplete < 5) {
                                        if (Thread.currentThread().isInterrupted())
                                            break;
                                        if (dataKey != null) {
                                            dfuLoadStatusBean = APMController.getDFULoadStatus(dataKey, uc);
                                            if (dfuLoadStatusBean == null || !dfuLoadStatusBean.isStrated()) {
                                                Thread.currentThread().sleep(5000);
                                                continue;
                                            } else {
                                                if (dfuLoadStatusBean.getNumberOfDFUsInOffer() != null && totalDFUsCnt != dfuLoadStatusBean.getNumberOfDFUsInOffer().getSecond()) {
                                                    totalDFUsCnt = dfuLoadStatusBean.getNumberOfDFUsInOffer().getSecond();
                                                    progressBar.setValue(10);
                                                }
                                                if (dfuLoadStatusBean.getDfuBaseCostStatusAndCountPair() != null && (baseCostLoadedCnt != totalDFUsCnt)) {
                                                    if (dfuLoadStatusBean.getDfuBaseCostStatusAndCountPair().getFirst()) {
                                                        baseCostLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;

                                                    } else {
                                                        baseCostLoadedCnt = dfuLoadStatusBean.getDfuBaseCostStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (dfuLoadStatusBean.getDfuBasePriceStatusAndCountPair() != null && (basePriceLoadedCnt != totalDFUsCnt)) {
                                                    if (dfuLoadStatusBean.getDfuBasePriceStatusAndCountPair().getFirst()) {
                                                        basePriceLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;

                                                    } else {
                                                        basePriceLoadedCnt = dfuLoadStatusBean.getDfuBasePriceStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (dfuLoadStatusBean.getDfusUDAStatusAndCountPair() != null && (udasLoadedCnt != totalDFUsCnt)) {`enter code here
                                                    if (dfuLoadStatusBean.getDfusUDAStatusAndCountPair().getFirst()) {
                                                        udasLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;

                                                    } else {
                                                        udasLoadedCnt = dfuLoadStatusBean.getDfusUDAStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (dfuLoadStatusBean.getDfusHISTStatusAndCountPair() != null && (histLoadedCnt != totalDFUsCnt)) {
                                                    if (dfuLoadStatusBean.getDfusHISTStatusAndCountPair().getFirst()) {
                                                        histLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;
                                                    } else {
                                                        histLoadedCnt = dfuLoadStatusBean.getDfusHISTStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (dfuLoadStatusBean.getDfusFCSTStatusAndCountPair() != null && (fcstLoadedCnt != totalDFUsCnt)) {
                                                    if (dfuLoadStatusBean.getDfusFCSTStatusAndCountPair().getFirst()) {
                                                        fcstLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;
                                                    } else {
                                                        fcstLoadedCnt = dfuLoadStatusBean.getDfusFCSTStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (totalDFUsCnt != 0.0)
                                                    progress = (((baseCostLoadedCnt / totalDFUsCnt * 80) + (basePriceLoadedCnt / totalDFUsCnt * 80) +
                                                            (udasLoadedCnt / totalDFUsCnt * 80) + (histLoadedCnt / totalDFUsCnt * 80) +
                                                            (fcstLoadedCnt / totalDFUsCnt * 80)) / 500d);

                                                if (progress != 0.0) {
                                                    progressBar.setValue((int) (progress * 100));
                                                    Thread.currentThread().sleep(500);
                                                }
                                            }
                                        }
                                    }
                                    return null;
                                }
                            });
                } catch (PrivilegedActionException e) {
                    e.printStackTrace();
                }
            }
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }
        public void finished() {
          }

        public void setBusinessObjectUserContext(BusinessObjectUserContext iUc) {
            uc = iUc;
        }
    }
   }
}

根据您所说的“卡住”的意思,我有一段代码,其中进程似乎停止了,因为相关的计时器被垃圾收集

换句话说,我必须将timer变量移动到一个对象上,这个对象将保持其实例不变


在我移动变量之前,没有运行时错误,它会停止工作。

这有一种试图从EventDispatch线程以外的其他线程在UI上完成某些事情的味道。在处理文档中未明确标记为线程安全的任何内容时,应确保使用类似SwingUtilities.invokeLater的内容。是的,您的权利此代码可能不是以线程安全的方式编写的。我们在这里做的错误事情是从swing worker的constructdoINBackground更新进度条。我已经删除了该代码,但问题仍然存在。谢谢您的回复。我希望处理进度条和对话框。当调用dispose方法时,但这可能不会发生。我认为这与巴巴多斯收集的任何成分无关。