Porting JDK1.5 ThreadLocal code to JDK1.4
I have below piece code which runs on JDK5
private static ThreadLocal<String> messages = new ThreadLocal<String>();
private static ThreadLocal<Boolean> dontIntercept = new ThreadLocal<Boolean>() {
@Override
protected Boolean initialValue() {
return Boolean.FALSE;
}
};
I wish to run it on JDK开发者_C百科1.4. Please advice what changes will be required
- Remove generics.
- Remove covariant return.
- Remove
@Override
annotation.
So
private static final ThreadLocal messages = new ThreadLocal();
private static final ThreadLocal dontIntercept = new ThreadLocal() {
protected Object initialValue() {
return Boolean.FALSE;
}
};
When using
- Cast value back to
Boolean
. - Unbox with
.booleanValue()
. - Box with
Boolean.valueOf
.
You will have to get rid of the generics and then cast the values appropriately when using the get and put methods. You'll also need to ensure that the boolean ThreadLocal is initialised correctly where it is used in the code.
If the program is correctly written to Java 1.4 behaviour, but uses Java 1.5+ notation, an approach we have used several times is to use Retroweaver to convert the compiled Java 1.5 byte code to Java 1.4 byte code.
http://retroweaver.sourceforge.net/
Others exist, but this is the one we have found to work the best.
You could always download the source code from sun and have a look at the ThreadLocal class.
Or use this link
精彩评论