N
Neptune
Gast
Hallo ich habe da ein Problem mit einer ehemaligen Klausuraufgabe.
Es heisst dort
Ich würde jetzt nach meiner Erfahrung sagen, das die Synchronisation ausreicht, aber das wäre doch etwas zu einfach denke ich.
Vielleicht kann mir ja jemand bei dem Problem helfen.
Es heisst dort
a) Ist die Synchronisation ausreichend, um Schreib/Schreib- und Schreib/Lese-
Konflikte auszuschließen? Falls nein, warum nicht?
Ich würde jetzt nach meiner Erfahrung sagen, das die Synchronisation ausreicht, aber das wäre doch etwas zu einfach denke ich.
Vielleicht kann mir ja jemand bei dem Problem helfen.
Code:
public class Test{
private String[] s1 = {"foo"};
private String[] s2 = {"bar"};
public void copy_s1_to_s2() {
synchronized(s1) {
synchronized(s2) {
s2[0]=s1[0];
}
}
}
public void copy_s2_to_s1() {
synchronized(s2) {
synchronized(s1) {
s1[0]=s2[0];
}
}
}
public static void main(String args[]) {
final Test t = new Test();
new Thread() {
public void run(){
t.copy_s1_to_s2();
}
}.start();
new Thread() {
public void run(){
t.copy_s2_to_s1();
}
}.start();
}
}