Dataflows provide safe channels to share data between producers and consumers. The most basic element of a dataflow is dataflow variables. You just create an object of the Dataflows class and then we can define variables on it. These variables have two important characteristics:

  • You can only set the value once
  • When a task tries to use the value of a dataflow's variable, its execution thread is blocked until the variable has a value

The benefits you can obtain with dataflow variables are:

  • You don't have race conditions
  • You don't need to use locks or other synchronization mechanisms explicitly
  • If there's a deadlock provoked by dataflow's variables, you can determine its cause

Let's see an example of how dataflow variables work. ...

Get Mastering Concurrency Programming with Java 9 - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.