Comment annuler un thread IO en cours d'exécution?
Je ne parviens pas à annuler un Thread d'écriture dans une boucle de fichiers Image.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| // ....
Thread t = new Thread(new Runnable() { public void run() {
try {
for(Iterator writers = ImageIO.getImageWritersByMIMEType(conversion); writers.hasNext();) {
ImageWriter w = (ImageWriter)writers.next(); //cache.buffer(w);
w.setOutput(ios = new FileImageOutputStream(outputFile)); //cache.buffer(ios);
w.write(bimg);
w.dispose();
ios.close();
}
} catch(IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(ImageMultiConverter.this, "An unknown error has occured while writing." + ex.getMessage()); }
});
t.start(); |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
switch(JOptionPane.showConfirmDialog(StatusPane.this, "Cancel all?")) {
case JOptionPane.CANCEL_OPTION:
break;
case JOptionPane.YES_OPTION:
synchronized(activeThreads){
for(Iterator i = activeThreads.values().iterator(); i.hasNext();) {
Thread t = (Thread)i.next();
boolean locked = true;
while(locked && t.isAlive()) {
try{
t.checkAccess(); // l'exception n'est pas levée ???
t.interrupt(); // ici le compilateur passe une fois sans réussir à arreter le thread !
locked = false;
} catch(SecurityException ex) {
locked = true;
}
}
}
}
break;
default:
}
}}); |
Quelqu'un a-t-il une solution pour interrompre une écriture en cours sur la sortie fichier? 8O