I'm trying to teach myself threads but can't seem to get synchronized() to work. This is what I'm doing:
In a class Test I'm declaring a variable
static String toggy = "false";
Then a runnable (MyRunnable) with this run method:
@Override
public void run() {
synchronized (toggy) {
System.out.println("toggy for " + name + " BEFORE = " + toggy);
int b = 0;
for(int i=0; i<1000; i++) {
b++;
}
System.out.println("toggy for " + name + " AFTER = " + toggy);
System.out.println("--------------------------------------------\n");
if(toggy.equals("false")) {
toggy = "true";
} else if(toggy.equals("true")) {
toggy = "false";
}
}
}
I'm testing this here:
for (int i = 0; i < 90; i++) {
Runnable task = new Test.MyRunnable(i+"");
Thread worker = new Thread(task);
worker.setName(String.valueOf(i));
// Start the thread, never call method run() direct
worker.start();
}
From what I understand, the synchronized block should prevent the value of "toggy" from changing while a certain thread is running. But when I run the program every now and then it does. What am I missing?