“传递参数”通过 ThreadLocal 可以吗?

发布于 2024-11-02 04:58:51 字数 826 浏览 6 评论 0原文

我正在构建一个 Java 网络库和一个使用它的应用程序。该库包括:

  • 一个 PacketSocket 接口,它具有发送和接收字节数据包的方法。
  • 它有两种实现方式,一种通过 TCP,一种通过 UDP。
  • 构建在 PacketSocket 之上的 ObjectConnection 类,用于处理对象到字节数据包的序列化。

该应用程序在 UDPPacketSocket 之上使用 RequestConnection。 UDPPacketSocket 实现的独特之处在于它支持指定每个数据包是否应保证传送。我希望能够从应用程序内部使用,但无法通过 ObjectConnection 和 PacketSocket 接口。

我当然可以向这些接口中的适用方法添加一个布尔保证参数,但最终(当有更多 PacketSocket 实现时)必须添加更多参数,这些参数仅特定于某些实现而被其他实现忽略。

相反,我认为可以使用 UDPPacketSocket 的静态线程本地属性来完成此操作,如下所示:

class Application {

  public void sendStuff() {

    // is stored in a ThreadLocal, so this code is still thread-safe
    UDPPacketSocket.setGuaranteed(true);

    try {
       myObjCon.send(...);
    } finally {
       // ... restore old value of guaranteed
    }

  }
}

您认为这样的方法怎么样?

I'm building both a Java networking library and an application which makes use of it. The library consists of:

  • An interface PacketSocket which has methods for sending and receiving packets of bytes.
  • Two implementations of it, one over TCP and one over UDP.
  • An ObjectConnection class which is built on top of a PacketSocket and handles serialization of objects to byte packets.

The application uses RequestConnection on top of a UDPPacketSocket. The UDPPacketSocket implementation is unique in that it supports specifying per packet whether delivery should be guaranteed. I would like to be able to use from within the application, but there is no way through the ObjectConnection and PacketSocket interfaces.

I could of course add a boolean guaranteed parameter to the applicable methods in those interfaces, but then I'd eventually (when there will be more implementations of PacketSocket) have to add many more parameters that are specific to certain implementations only and ignored by others.

Instead I though I could do it with a static thread-local property of UDPPacketSocket, like so:

class Application {

  public void sendStuff() {

    // is stored in a ThreadLocal, so this code is still thread-safe
    UDPPacketSocket.setGuaranteed(true);

    try {
       myObjCon.send(...);
    } finally {
       // ... restore old value of guaranteed
    }

  }
}

What do you think of an approach like that?

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

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

发布评论

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

评论(4

北恋 2024-11-09 04:58:51

我认为这是一个丑陋的黑客,但有时它是唯一的选择,特别是如果您通过多层代码“传递”一个值并且您无法轻松修改该代码。

如果可以的话我会避免它。如果可能的话,更好的选择是具有以下内容

 myObjCon.sendGuaranteed(...);

I think its an ugly hack, however sometimes it is only option, esp if you are "passing" a value through many layers of code and you cannot easily modify that code.

I would avoid it if you can. A better option would be to have the following, if possible

 myObjCon.sendGuaranteed(...);
叹倦 2024-11-09 04:58:51

我同意这是一个丑陋的黑客行为。它会起作用,但你最终可能会后悔这样做。

我将通过使用 Properties 对象来传递各种 PacketSocket 实现参数来处理此问题。如果这令人不快,请定义一个 PacketSocketParameters 接口,其中包含不同类型 PacketSocket 的实现类层次结构。

I agree that this is an ugly hack. It will work, but you may end up regretting doing it.

I'd deal with this by using a Properties object to pass the various PacketSocket implementation parameters. If that is unpalatable, define a PacketSocketParameters interface with a hierarchy of implementation classes for the different kinds of PacketSocket.

江南烟雨〆相思醉 2024-11-09 04:58:51

我会推荐某种“性能特征”参数,可能类似于 Properties 实例。然后,每个 impl 都可以使用自己的任意属性(例如,当前 impl 的“保证”)。请注意,您可以通过使用 Properties 上的对象方法(例如 get() 而不是 getProperty())或使用直接的 Map 实例来避免字符串解析。那么你的值可能是真实的对象(例如布尔值)。

i'd recommend some sort of "performance characteristics" parameter, maybe something like a Properties instance. then, each impl could use their own, arbitrary properties (e.g. "guaranteed" for your current impl). note, you can avoid string parsing by using the object methods on Properties (e.g. get() instead of getProperty()) or using a straight Map instance. then your values could be true objects (e.g. Boolean).

旧人九事 2024-11-09 04:58:51

因为我们知道它是 UDP,所以我们可以对层进行去抽象并访问具体内容

( (UDPSocket)connection.getSocket() ).setGuaranteed(true);

since we know it's a UDP, we can de-abstract the layers and access the concrete stuff

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