简体   繁体   English

Java线程化JavaDoc

[英]Java threading JavaDoc

I have written a method which should only be called on a particular thread. 我编写了一个只应在特定线程上调用的方法。 Is there a standard annotation or note which should be added to the method's javadoc to denote this? 是否有标准注释或注释应添加到方法的javadoc中以表示这一点?

Don't know of any such standard annotations. 不知道任何这样的标准注释。 Java Concurrency in Practice deals with the question in its section 4.5: Documenting Synchronization Policies . Java Concurrency in Practice在第4.5节“ 记录同步策略”中讨论了该问题。 A few hints which hopefully help you make your documentation clear and useful: 一些提示可以帮助您使文档清晰有用:

At the very least, document the thread safety guarantees made by a class. 至少,记录一个类所做的线程安全保证。 Is it thread-safe? 它是线程安全的吗? Does it make callbacks with a lock held? 它是否会锁定回调? Are there any specific locks that affect its behavior? 是否存在影响其行为的特定锁定? Don't force clients to make risky guesses. 不要强迫客户进行风险猜测。 If you don't want to commit to supporting client-side locking, that's fine, but say so. 如果你不想承诺支持客户端锁定,那很好,但是这样说。 If you want clients to be able to create new atomic operations on your class, as we did in Section 4.4, you need to document which locks they should acquire to do so safely. 如果您希望客户端能够在您的类上创建新的原子操作,就像我们在4.4节中所做的那样,您需要记录他们应该安全地获取哪些锁。 If you use locks to guard state, document this for future maintainers, because it's so easy - the @GuardedBy annotation will do the trick. 如果您使用锁来保护状态,请将此文档记录给未来的维护者,因为它很容易 - @GuardedBy注释可以解决问题。 If you use more subtle means to maintain thread safety, document them because they may not be obvious to maintainers. 如果您使用更微妙的方法来维护线程安全,请记录它们,因为它们对维护者来说可能并不明显。

They also use some annotations, which are not standard, but recommended by them (see Appendix A). 他们还使用了一些注释,这些注释不是标准的,但是由他们推荐(见附录A)。 However, for methods they only offer variations of @GuardedBy , which is not applicable to your case. 但是,对于方法,它们仅提供@GuardedBy变体,这不适用于您的情况。

I recommend just clearly documenting the requirement in plain Javadoc. 我建议只清楚地记录普通Javadoc中的需求。

In my opinion, the best way to handle it is to remove the requirement. 在我看来,处理它的最好方法是删除要求。 Change the method to private and rename it slightly by adding the word Workload or Internal or something. 将方法更改为private并通过添加单词WorkloadInternal或其他内容将其重命名。 Then create a new public method with the same signature. 然后使用相同的签名创建一个新的公共方法。 Have this method check to see if you are on the correct thread. 请检查此方法是否在正确的线程上。 If you are, you can just execute the private method. 如果是,您可以只执行私有方法。 If not, then schedule the private method to be executed on the correct thread. 如果没有,则安排在正确的线程上执行私有方法。 This way, the user of the API doesn't have to worry about threading and can just call the method. 这样,API的用户不必担心线程,只需调用该方法即可。

Then, there is nothing to specify in the javadoc, although it is still useful to include this information in the description of the public and private methods. 然后,在javadoc中没有要指定的内容,尽管在公共和私有方法的描述中包含此信息仍然很有用。

This is the pattern I use when I need something executed on the EDT: 当我需要在EDT上执行某些操作时,这是我使用的模式:

/**
 * Executes something on the EDT with the crazy argument specified.  If this is
 * called outside of the EDT, it will schedule the work to be done on the EDT
 * as soon as possible. The actual work of this method is found in
 * {@link #executeSomethingInternal(int)}.
 *
 * @argument crazyArgument some crazy argument
 */
public void executeSomething(int crazyArgument) {
  if (SwingUtilities.isEventDispatchThread()) {
    this.executeSomethingInternal(crazyArgument);
  } else {
    Runnable r = new Runnable() {
      private int crazyArgument;

      public Runnable setCrazyArgument(int crazyArgument) {
        this.crazyArgument = crazyArgument;
        return this;
      }

      @Override
      public void run() {
        this.OuterClass.executeSomethingInternal(this.crazyArgument);
      }
    }.setCrazyArgument(crazyArgument);
    SwingUtilities.invokeLater(r);
  }
}

/**
 * This method actually does the work.  It is guaranteed by this class to
 * always get called on the EDT.  Users of this API should call
 * {@link #executeSomething(int)}.
 */

private void executeSomethingInternal(int crazyArgument) {
  // do work here
}

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

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