potyczki psyche i soma buy soma soma bartın arası

buy ambien Maine buy ambien xanax bar and ambien

does tramadol decrease blood pressure buy tramadol overdosing on tramadol symptoms

ambien muscle soreness buy ambien online ambien in english

flexeril hydrocodone and tramadol tramadol online overnight is toradol like tramadol

vyvanse drug interactions xanax buy xanax online without prescription xanax and the elderly

xanax relieves back pain xanax pill headache morning after xanax

tramadol sulfa allergy buy tramadol online drugs stronger than tramadol

abc news ambien driving ambien for sale taking ambien with diphenhydramine

sleepwalking talking ambien zolpidem 10 mg can u overdose ambien

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 ThreadedIndexer.run 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 5 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 *