===== Multithreading =====
^What ^Where ^
|Introduction | [[http://www.vogella.com/articles/JavaConcurrency/article.html|vogella]] |
|Whereever possible use the safe Structures from | [[http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html|concurrent package]] |
=== PitFalls ===
- Do not use **TimeStamps** to synchronize Threads. Therere can be more than 1 action every millisecond - what to do with equal TimeStamps?
=== Concurrent Package ===
Das [[http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html|Concurrent Package]] bietet viele fertige Multithreading Klassen.
^Class ^ What ^
| [[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html|Timer]] | Can execute TimerTasks(Runnables) after a Timeout. Can be canceled only once. |
| [[http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/FutureTask.html|Future]] | Can compute something asynchronously. The result, when ready, is available throught get() |
=== ForkJoin ===
A framework to execute parallel task. The mechanism is [[http://www.cs.washington.edu/homes/djg/teachingMaterials/spac/grossmanSPAC_forkJoinFramework.html|described here]].
[[http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fwidgets%2FDisplay.html|Display.readAndDispatch()]]
//A nice way to stop the UI thread, in order to wait for a job, without blocking the UI Jobs coming from other Threads
while(job.getResult()==null) {
Display.getDefault().readAndDispatch();
}
=== Try-Catch is Bypassed in nested threads ===
On every thread there should be an own Exception-handling. Putting a new Thread creation into try-catch block won't catch the Exceptions.
//ATTENTION: the exception is not catched!
try{
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("Thread");
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
System.out.println("ThreadGUI");
throw new NullPointerException();
}
});
}
};
Thread t = new Thread(r);
t.start();
}catch(NullPointerException e){
//nothing
}
System.out.println("Ende");