This looks like the simplest runnable I could imagine
public class StringShower implements Runnable{
private volatile boolean running = true;
private String text;
public StringShower(String text){
this.text = text;
}
@Override
public void run() {
while (running) {
try {
System.out.println("Current text" + text);
if(text != null){
showText(text);
}
Thread.sleep((long) 1000);
} catch (Exception e) {
running = false;
}
}
}
private void showText(String text2) {
System.out.println("Current Text " + text2);
}
public void stopRunning() {
this.running = false;
}
That is the class starting it
public class ChangeString {
private static String changeme;
public static void main(String[] args) {
Thread thread1 = new Thread(new StringShower(changeme));
thread1.start();
changeme = "changed";
}
}
After reading many examples, I can't still figure out why the String
inside the thread is never updated. Can anyone explain why?