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

发布于 2024-08-20 12:59:29 字数 362 浏览 7 评论 0原文

我有以下在 JDK5 上运行的代码,

private static ThreadLocal<String> messages = new ThreadLocal<String>();
private static ThreadLocal<Boolean> dontIntercept = new ThreadLocal<Boolean>() {
    @Override
    protected Boolean initialValue() {
        return Boolean.FALSE;
        }
};

我希望在 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 JDK1.4. Please advice what changes will be required

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月染过的梦 2024-08-27 12:59:29
  • 删除泛型。
  • 删除协变返回。
  • 删除@Override注释。

因此,

private static final ThreadLocal messages = new ThreadLocal();
private static final ThreadLocal dontIntercept = new ThreadLocal() {
    protected Object initialValue() {
        return Boolean.FALSE;
    }
};

当使用

  • 将值转换回布尔值时。
  • 使用 .booleanValue() 取消装箱。
  • 带有 Boolean.valueOf 的框。
  • 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.
久伴你 2024-08-27 12:59:29

您必须摆脱泛型,然后在使用 get 和 put 方法时适当地转换值。您还需要确保布尔值 ThreadLocal 在代码中使用时正确初始化。

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.

森林散布 2024-08-27 12:59:29

如果程序正确编写为 Java 1.4 行为,但使用 Java 1.5+ 表示法,我们多次使用的方法是使用 Retroweaver 将编译后的 Java 1.5 字节代码转换为 Java 1.4 字节代码。

http://retroweaver.sourceforge.net/

其他也存在,但这是我们发现可以工作的一个最好的。

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.

单身狗的梦 2024-08-27 12:59:29

您始终可以从 sun 下载源代码并查看 ThreadLocal 类。

或者使用这个链接

You could always download the source code from sun and have a look at the ThreadLocal class.

Or use this link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文