soma e subtracao de notacao cientifica soma no prescription shokugeki no soma scan 25

the grill soma buy soma cubo soma figuras resueltas

do doctors recommend phentermine buy phentermine phentermine generic brand name

ambien early waking ambien without prescription ambien tagamet

how many melatonin equals ambien buy ambien i have to take ambien every night

valium and root canal diazepam 5mg buy valium online Visalia

pierce the veil may 10 soma buy soma online manga de shokugeki no soma

is there valerian in valium buy valium online can you take valium under your tongue

buy retin a online no prescription buy retin-a without prescription retin-a micro reviews

ambien side effects itching buy zolpidem online ambien online shopping

Fork-Join Multi Threading in Java JDK 6

June 11, 2012 one comment Posted in Tutorial

Multi threading is the current trend. Almost every computer we bought had a multi-core processsor. However turning our code from a single thread to multi-thread is not an easy task. Not all algorithm can directy converted to a “multi-thread” version.

One of the simplest solution is to fork-join our algorithm. As the name suggest, we first need to “fork” (split) our algorithm into several parts, process each part in a independent running  thread. After all process were finished we  “join” ( combined ) all the process again.

The Fork-Join Theorem
Figure 1 : The Fork-Join Theorem

I first meet the necessity to use this algorithm when i have a class project to index several hundreds files using Apache Lucene. Indexing file is a trivial process. All we need to do is open , scan , and index the file.

However this indexing is a sequential pattern which is so uneffective. Its only utilize one processor and i want to make the process more efecient by fork-join the task.

The sample in this post is a simplified example of how indexing process was going on. But the basic is still the same. To speed up the proccess i’am goting to spread the indexing proccess using the fork-join algorithm.

First i’am going to split the file into several arrays and than running the each array processing on an independent thread. In the example i’am using two array, one for the odd file, and the other for even number.

If you want the process more efficient you can create more fork by using more than 2 array , this is just an example anyway.

We also need to define a class which implement runnable interface to do the indexing task.

Spliting the task is not that difficult, the crucial part in this multi-threaded java application is about how to join all the running thread together.

We have to remember that each main program that we run is also running its own thread (Look at figure 1). The challenge is  how to tell the main program if all the other thread is already finish the  indexing process. Fortunelly there is already a function to do this fork-join proccess.

The  function  actually come from my Lecturer in UNSW , in which i believe is derived from some java multi-threaded library in the web ( I forgot the exact Author)

Instead of calling independently we will use this function instead. The best part of this function is it will join the main thread and all its child thread.

So that’s it , the fork-join is really helpfull to fully utilize our multi-core processor.

Full Source Code of this post :

NB :

By the time of the writing of this post, Java 7 actually also provide us with a built in library to fork-join the multiple thread.

One comment

  1. learn to play guitar 4 years ago

    What’s up, its nice piece of writing on the topic of media print, we all understand media is a wonderful source of data.

Leave a Comment

Your email address will not be published. Required fields are marked *