简体   繁体   English

将JDK1.5 ThreadLocal代码移植到JDK1.4

[英]Porting JDK1.5 ThreadLocal code to JDK1.4

I have below piece code which runs on JDK5 我下面有在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 JDK1.4. 我希望在JDK1.4上运行它。 Please advice what changes will be required 请告知需要哪些更改

You will have to get rid of the generics and then cast the values appropriately when using the get and put methods. 您将必须摆脱泛型,然后在使用get和put方法时适当地转换值。 You'll also need to ensure that the boolean ThreadLocal is initialised correctly where it is used in the code. 您还需要确保布尔ThreadLocal在代码中使用的位置正确初始化。

  • Remove generics. 删除泛型。
  • Remove covariant return. 删除协变量收益。
  • Remove @Override annotation. 删除@Override注释。

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 . 将值转换回Boolean
  • Unbox with .booleanValue() . 使用.booleanValue()
  • Box with Boolean.valueOf . 带有Boolean.valueOf框。

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. 如果程序正确地编写为Java 1.4行为,但是使用Java 1.5+表示法,则我们使用过多次的方法是使用Retroweaver将编译后的Java 1.5字节代码转换为Java 1.4字节代码。

http://retroweaver.sourceforge.net/ 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. 您总是可以从sun下载源代码,并查看ThreadLocal类。

Or use this link 或使用此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM