Fork/Join framework in Java
The JDK 7 Fork/Join framework is a set of classes and interfaces that take advantage of the multiprocessor architecture of modern computers. It is designed to perform tasks that can be recursively broken down into small sub-tasks that can be done in parallel.
- Fork stage: a large task is split into several smaller subtasks, which in turn are also split into smaller ones. And so on until the task becomes trivial and can be solved in a sequential way.
- Join stage: then (optionally) there is a "folding" process - solutions of subtasks are united in some way until the solution of the whole problem is obtained.
The solution of all subtasks (including the division into subtasks itself) occurs in parallel.
For some tasks, the Join step is not required. For example, for parallel QuickSort, the array is recursively divided into smaller and smaller ranges until it degenerates into the trivial case of 1 element. Although, in a sense, Join will be necessary here as well, since there is still a need to wait until all subtasks are completed.
Another great advantage of this framework is that it uses a work-stealing algorithm: threads that have finished executing their own subtasks can "steal" subtasks from other threads that are still busy.
Semaphore
Semaphore is a new type of synchronizer: a counter semaphore that implements the Semaphore synchronization pattern. Access is controlled using a counter: the initial value of the counter is set in the constructor when creating the synchronizer, when a thread enters a given block of code, the counter value decreases by one, when the thread leaves it, it increases. If the counter value is zero, then the current thread is blocked until someone leaves the protected block. Semaphore is used to protect expensive resources that are only available in limited quantities, such as connecting to a pooled database.
Read also:
Comments
Post a Comment