Gegenüberstellung Ada95 – Java
Ada95: Tasks
Defintion eines Tasks
task Hund;
task body Hund is begin Print("wau"); Print("wau"); end Hund;
Starten eines Tasks Erfolgt im Normalfall gleichzeitig mit dem Hauptprogramm oder durch Entrys
task type Hund is entry Los; end Hund;
task body Hund is begin accept Los; Print("wau"); Print("wau"); end Hund;
|
Java: Threads
Defintion eines Threads
class Hund extends Thread { public void run() { System.out.println("wau"); System.out.println("wau"); } // run } /* end Hund */
Starten eines Threads Muß dediziert durch Aufruf von Hund.start() erfolgen.
|
Semaphore
sem1 : Semaphore := 1;
Wait(sem1); kritischer Bereich Signal(sem1);
|
Semaphore
Counting-und Binary-Semaphoren sind nicht direkt in Java implementiert. Eine Binary-Semaphore kann allerdings so relisiert werden:
final object sem1;
synchronized(sem1) { kritischer Bereich }
|
Mögliche Realisierung einer Semaphore in JAVA
public abstract class Semaphore
{
protected int value = 0;
protected Semaphore() {value = 0;}
protected Semaphore(int initial)
{
if (initial < 0) throw new IllegalArgumentException("initial<0");
value = initial;
}
public synchronized void P()
{
value--;
if (value < 0)
{
while (true)
{
try
{
wait();
break;
}
catch (InterruptedException e)
{
System.err.println ("Semaphore.P(): InterruptedException, wait again");
if (value >= 0) break;
else continue;
}
}
}
}
public synchronized void V()
{
value++;
if (value <= 0) notify();
}
}
Links
http://www.mcs.drexel.edu/~shartley/ConcProgJava/