synchronized, volatile, etc…
Disclaimer: Dieser Thread wurde aus dem alten Forum importiert. Daher werden eventuell nicht alle Formatierungen richtig angezeigt. Der ursprüngliche Thread beginnt im zweiten Post dieses Threads.
Disclaimer: Dieser Thread wurde aus dem alten Forum importiert. Daher werden eventuell nicht alle Formatierungen richtig angezeigt. Der ursprüngliche Thread beginnt im zweiten Post dieses Threads.
Sichtbarkeitssynchronisationen
ich habe ein paar Fragen zu Sichtbarkeitssynchronisation, am Beispiel von einem Schreibtischlauf (Klausur 19.2.2013, A4)
hier der Link: https://www2.cs.fau.de/teaching/SS2014/PFP/organisation/oldexams/secure/13-02-19_klausur.pdf
Es wäre wirklich sehr hilfreich, mir jemand die Fragen beantworten kann
falls was von meinen Aussagen nicht stimmt, bitte ich um Feedback
das meiste davon hab ich ausprobiert:
public class BarrierTest {
public static volatile int a= 0 , b= 0;
public static CyclicBarrier barrier = new CyclicBarrier(4);
public static CountDownLatch latch = new CountDownLatch(3);
public static Object lock = BarrierTest.class;
public static class MyThreadA extends Thread{
public void run(){
try{
synchronized (lock){
a++;
System.out.println("a = "+ a);
}
barrier.await();
if(b == 0){
synchronized(lock){
if(b == 0){
b = 1;
}
}
System.out.println("b = " + b);
}
latch.countDown();
}catch (Exception e){}
}
}
public static class MyThreadB extends Thread{
public void run(){
try{
synchronized(BarrierTest.class){
a++;
System.out.println("a = "+ a);
}
barrier.await();
if(b == 0){
synchronized (lock) {
if (b == 0){
b = 2;
}
}
System.out.println("b = "+b);
}
latch.countDown();
} catch (Exception e){}
}
}
public static void main (String[] args) throws Exception{
Thread tA_1 = new MyThreadA();
Thread tA_2 = new MyThreadA();
Thread tB_3 = new MyThreadB();
Thread tB_4 = new MyThreadB();
tA_1.start();
tA_2.start();
tB_3.start();
while(a < 3){}
System.out.println("Trhead 1 , 2 , 3 started.");
tB_4.start();
latch.await();
System.out.println("Thread 4 started.");
tA_1.join();
tA_2.join();
tB_3.join();
tB_4.join();
}
}
danke für die schnelle Hilfe