@Override @SuppressWarnings("SleepWhileHoldingLock") public void run() { try { // initialize the statusbar status.removeAll(); JProgressBar progress = new JProgressBar(); progress.setMinimum(0); progress.setMaximum(doc.getLength()); status.add(progress); status.revalidate(); // start writing Writer out = new FileWriter(f); Segment text = new Segment(); text.setPartialReturn(true); int charsLeft = doc.getLength(); int offset = 0; while (charsLeft > 0) { doc.getText(offset, Math.min(4096, charsLeft), text); out.write(text.array, text.offset, text.count); charsLeft -= text.count; offset += text.count; progress.setValue(offset); try { Thread.sleep(10); } catch (InterruptedException e) { Logger.getLogger(FileSaver.class.getName()).log(Level.SEVERE, null, e); } } out.flush(); out.close(); } catch (IOException e) { final String msg = e.getMessage(); SwingUtilities.invokeLater( new Runnable() { public void run() { JOptionPane.showMessageDialog( getFrame(), "Could not save file: " + msg, "Error saving file", JOptionPane.ERROR_MESSAGE); } }); } catch (BadLocationException e) { System.err.println(e.getMessage()); } // we are done... get rid of progressbar status.removeAll(); status.revalidate(); }
public void testDeadlock49178() throws Exception { // open the document final StyledDocument docu = support.openDocument(); // start closing it Thread closing = new Thread( new Runnable() { public void run() { support.close(false); // will block in notifyUnmodified() closingDone = true; } }); closing.start(); Thread processing = new Thread( new Runnable() { boolean second = false; public void run() { if (!second) { second = true; docu.render(this); // NbDocument.runAtomic(docu, this); } else { // inside readLock support.createPositionRef(0, Position.Bias.Forward); processingDone = true; } } }); synchronized (waitLock) { while (!inWait) waitLock.wait(); } processing.start(); Thread.sleep(1000); synchronized (waitLock) { shouldWait = false; waitLock.notifyAll(); } closing.join(10000); processing.join(10000); assertNull("No exception thrown", exception); assertTrue("Closing thread finished", closingDone); assertTrue("Processing thread finished", processingDone); }
@Override public Void doInBackground() { int current = 0; int lengthOfTask = 100; while (current <= lengthOfTask && !isCancelled()) { try { // dummy task Thread.sleep(50); } catch (InterruptedException ie) { return null; } setProgress(100 * current / lengthOfTask); current++; } return null; }
private synchronized void closeAll() { try { // Close all child window WindowManager.closeAll(); // Remove all child component pnlThread.setVisible(false); pnlUser.setVisible(false); pnlThread.removeAll(); clearAll(txtBoard); tblUser.setData(new Vector()); Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); MessageBox.showMessageDialog(this, e, Global.APP_NAME, MessageBox.ERROR_MESSAGE); } }
/** Cancel all active downloads. */ public void cancelActiveDownloads() { for (Component c : this.monitorPanel.getComponents()) { if (c instanceof DownloadMonitorPanel) { if (((DownloadMonitorPanel) c).thread.isAlive()) { DownloadMonitorPanel panel = (DownloadMonitorPanel) c; panel.cancelButtonActionPerformed(null); try { // Wait for thread to die before moving on long t0 = System.currentTimeMillis(); while (panel.thread.isAlive() && System.currentTimeMillis() - t0 < 500) { Thread.sleep(10); } } catch (Exception ignore) { } } } } }
/* * Main task. Executed in background thread. */ @Override public String doInBackground() throws Exception { if (myTaskName.equals("Get DownloadFile Info")) { if (!isCancelled()) { getFileInfo(); } } if (myTaskName.equals("Download Remote")) { if (!isCancelled()) { long theFileSize = getFileSize(); if (theFileSize != -1) { sendDownloadMessage(); int thePollInterval = Integer.parseInt(myProperties.getProperty("POLLINTERVAL")); long theCurrentTime = System.currentTimeMillis(); while (getStatus(theFileSize) && !isCancelled()) { if ((System.currentTimeMillis() - theCurrentTime) < thePollInterval * 1000) { Thread.sleep(thePollInterval * 1000 - System.currentTimeMillis() + theCurrentTime); } theCurrentTime = System.currentTimeMillis(); } } } } if (myTaskName.equals("Download Local Base")) { if (!isCancelled()) { downloadFileFromBase(); } } if (myTaskName.equals("Clean Remote Temp")) { if (!isCancelled()) { CleanRemoteTempDir(); } } if (myTaskName.equals("Clean Remote Base")) { if (!isCancelled()) { CleanRemoteBaseDir(); } } if (myTaskName.equals("Split Remote")) { if (!isCancelled()) { splitFileFromBase(); } } if (myTaskName.equals("Email Remote")) { if (!isCancelled()) { emailFileFromBase(); } } if (myTaskName.equals("Join Local")) { if (!isCancelled()) { joinFileFromLocalBase(); } } /** if(!isCancelled()) { CleanRemoteTempDir(); }* */ /** if(!isCancelled()) { getFileList(); }* */ /** if(!isCancelled()) { verifyAndSync(); }* */ return "Done"; }