1

Strangely, Java Doc says:

These methods are designed to be used as tools for creating higher-level synchronization utilities, and are not in themselves useful for most concurrency control applications.

Is there any example/scenario to illustrate JavaSupport is useful?

xmllmx
  • 39,765
  • 26
  • 162
  • 323
  • Not sure if this shouldn't better go to programmers.stackexchange.com ... in other words: what is the problem that you intend to solve? – GhostCat Jun 13 '16 at 11:53
  • 1
    Just emphasizing what Kayaman already said; `LockSupport` _is_ quite useful, but most of the clever things you can do with it already been done in open-source libraries. if you find yourself tempted to use `LockSupport`, you should first try to find some higher-level synchronization class that would better fit or simplify your design. Chances are good that you will find one. – Solomon Slow Jun 13 '16 at 13:42

2 Answers2

4

It's used by many classes in the java.util.concurrent package, as explained in the Javadoc. You're not supposed to (or rather you don't need to) use it unless you're creating your own high-level concurrency classes, since there are plenty of ready easy-to-use classes that can accomplish what you want, without going down to the "bare metal".

Kayaman
  • 72,141
  • 5
  • 83
  • 121
2

Is there any example/scenario to illustrate [LockSupport] is useful?

Look through the source code of the Java concurrency classes to find where the class is used:

  • These are examples of the kind of scenario where LockSupport is useful.

  • They probably also illustrate the comment in the javadoc that you quoted. These primitives are low-level, and difficult to use directly.

To get you started, LockSupport is used in the following classes in the standard class libraries (Java 8):

  • java.util.concurrent.Exchanger
  • java.util.concurrent.CompletableFuture
  • java.util.concurrent.SynchronousQueue
  • java.util.concurrent.ConcurrentHashMap
  • java.util.concurrent.locks.StampedLock
  • java.util.concurrent.locks.AbstractQueuedSynchronizer
  • java.util.concurrent.locks.AbstractQueuedLongSynchronizer
  • java.util.concurrent.LinkedTransferQueue
  • java.util.concurrent.FutureTask
  • java.util.concurrent.ForkJoinPool
  • java.util.concurrent.Phaser
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216