is tramadol like a percocet buy tramadol online norco or tramadol

ambien side effects muscle aches buy ambien zolpidem buy Mississippi

.5mg xanax withdrawal symptoms buy xanax online no prescription dose massima xanax gocce

valium epilepsie hond buy valium online valium mylan 477

lethal dose ambien ambien medication order ambien Pomona

valium online Las Vegas buy valium online does valium and ativan test the same

soma seeds lavender review order soma little soma berlin

tramadol schedule florida buy tramadol no prescription tramadol hcl white pill

why does tramadol make my heart race cheap tramadol online tramadol overdose emedicine

medicina tramadol 50 mg buy tramadol buy tramadol online Cape Coral

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 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 *