"it tells me I need to change String to final which wont work then"
It seems to hint me that your problem is not about threading, but something related (anonymous) inner class access to variables in enclosing scope.
I bet what you are doing is something like this:
class Foo {
public void foo() {
String myString = "abc";
Thread t = new Thread(
new Runnable() {
@Override
public void run() {
myString = "xyz";
}
}
};
t.start();
t.join();
System.out.println("result :" + myString);
}
}
You will find compiler is complaining for "myString should be final" something like that. It has nothing to do with multi-threading. It is simply that for anonymous inner class, it have access to only final variables in the enclosing scope.
Given String being immutable, one of the way to solve by keeping your original code structure is, to have some holder of String which is final. String array is a possible choice, or you can make a holder as simple as this:
public class StringHolder {
String value;
public StringHolder(String value) {
this.value = value;
}
public void setValue(String value) {
this.value = value;
}
public value getValue() {
return this.value;
}
}
and change your code to something like this:
class Foo {
public void foo() {
String myString = "abc";
final StringHolder myStringHolder = new StringHolder(myString);
Thread t = new Thread(
new Runnable() {
@Override
public void run() {
myStringHolder.setValue("xyz");
}
}
};
t.start();
t.join();
System.out.println("result: " + myStringHolder.getValue());
}
}
However, this is not the recommended way for getting result from thread.
I strongly suggest you change your code structure, and look into usage of Future (and related classes).