The Fork/Join Framework

Java 5 and Java 6 added several concurrency constructs to the java.util.concurrent package to help developers deal with problems involving advanced level of concurrency. The executor framework was one of the several great additions in Java 5. Java 7 added a new implementation of the executor service, which is known as a fork/join framework. The focus of the fork/join framework is to solve those problems efficiently, which may use the divide-and-conquer algorithm, by taking advantage of the multiple processors on a machine. Before Java 7, we had several Java constructs to help us solve the problems that involved concurrency. The fork/join framework helps us solve the problems that involve parallelism. Typically, the fork/join framework is suitable in a situation where:

  • A task can be divided in multiple subtasks that can be executed in parallel.
  • When subtasks are finished, the partial results can be combined to get the final result.

The fork/join framework creates a pool of threads to execute the subtasks. When a thread is waiting on a subtask to finish, the fork/join framework uses that thread to execute other pending subtasks of other threads. The technique of an idle thread executing other thread’s task is called work stealing. The fork/join framework uses a work-stealing algorithm to enhance the performance.

Click here to read the complete post.


Translucent Window in Java 7

Let us define three terms – transparent, translucent, and opaque, before we discuss how to use a translucent window in Swing. If something is transparent, you can see through it. For example, clear water is transparent. If something is opaque, you cannot see through it. For example, a concrete wall is opaque. If something is translucent, you can see through it, but not clearly. If something is translucent, it partially allows light to pass through. For example, a plastic curtain is translucent. The terms, “transparent” and “opaque”, describe two opposite states, whereas the term “translucent” describes a state between “transparent” and “opaque”.

Java 7 lets you define the degree of translucency of a window, e.g., a JFrame. A 90% translucent window is 10% opaque. The degree of translucency for a window can be defined using the alpha value of the color component for a pixel. You can define the alpha value of a color using the constructors of the Color class as shown below.

Color(int red, int green, int blue, int alpha)

Color(float red, float green, float blue, float alpha)

The value for the alpha argument is specified between 0 and 255, when the color components are specified in terms of int values. For the float type arguments, its value is between 0.0 and 1.0. The alpha value of 0 or 0.0 means transparent (100% translucent or 0% opaque). The alpha value of 255 or 1.0 means opaque (0% translucent or not transparent).

Java 7 supports three forms of translucency in a window, which are represented by the following three constants in the WindowTranslucency enum.

  • PERPIXEL_TRANSPARENT: In this form of translucency, a pixel in a window is either opaque or transparent. That is, the alpha value for a pixel is either 0.0 or 1.0.
  • TRANSLUCENT: In this form of translucency, all pixels in a window have the same translucency, which can be defined by an alpha value between 0.0 and 1.0.
  • PERPIXEL_TRANSLUCENT: In this form of translucency, each pixel in a window can have its own alpha value between 0.0 and 1.0. This form lets you define the translucency in a window on a per pixel basis.

Click here to read the complete post.