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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| public class SimpleTest {
private static final class SimpleThread implements Runnable {
@Override
public synchronized void run() {
System.out.println("Début du traitement !");
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.open(MessageDialog.ERROR, null, "Erreur",
"Une erreur est survenu.", SWT.NONE);
System.out.println("Libération du verrou !");
SimpleThread.this.unlock();
}
});
try {
wait();
} catch (final InterruptedException iException) {
iException.printStackTrace();
}
System.out.println("Traitement fini !");
}
public synchronized void unlock() {
notify();
}
}
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
final Button button = new Button(shell, SWT.PUSH);
final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
button.setLayoutData(gridData);
button.setText("Lancer la tâche");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
new Thread(new SimpleThread()).start();
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
} |
Partager