CodeBlocks

Sunday, 17 March 2013

Java simple threading library

Simple threading in Java using JavaCP library

In this post I'm going to talk about a simple way to achieve multi threading and parallel computation using a simple library I've assembled based off of the work of Weimin Xiao and the rest at this stack overflow post.

I take no credit in any of the code written in the library it self(the code below is mine) I merely just assembled a simple library and have used it to successfully achieve embarrassingly thread execution. If you're not familiar with embarrassing threading, it is threading tasks that in no way affect the outcome of each other. E.g having to process a large library of files, this task can be easily threaded by having each task processed in a separate thread. Which is primarily what I've used it for in processing Photos and videos to find perceptually similar content. I'll discuss video and photo matching algorithm in a later post.

1. Where to get the library? here,
2. How to use the library?
  1. Place the JavaCP.jar anywhere you want we you can find it.
  2. In your IDE(eclipse etc..) add the JavaCP.jar as a library
    • In ecplise this is done by right clicking on the project root folder in the package explorer window and selecting properties
    • In the properties window select Java Build Path
    • Select the Libraries tab
    • Click on Add External JARs, navigate to where you placed the JavaCP.jar and select ok.

The library is now ready to be used.
The library is able to thread 2 different things. ForEach Loops and concurrent method execution.

In your program import the library

//Import the library
import JavaCP.*;

Example code to run Tasks
//Using it to execute Multiple methods concurrently
//The number of threads created depends on the number of cores available
Parallel.Tasks(new Task []
{
 
 //task-1
 new Task() {public void run()
 {
  loops.runLoop(1000000000);
 }},
   
 //task-2
 new Task() {public void run()
 {
  loops.runLoop(1000000000);
 }},
 
 new Task() {public void run()
 {
  loops.runLoop(1000000000);
 }},
 
 new Task() {public void run()
 {
  loops.runLoop(1000000000);
 }}
});
In this example the method loops.runLoop(int _value) will be executed multiple times along side other instance of the loops.runLoop(int _value).

A more practical use would be something like this, where each method as to do something time consuming since creating threads themselves are taxing.
Parallel.Tasks(new Task []
{
 //task-1
 new Task() {public void run()
 {
  feature[0] = Variance(red,0);   
 }},
   
 //task-2
 new Task() {public void run()
 {
  feature[1] = Kurtosis(red,0);
 }},   
 //task-3
 new Task() {public void run()
 {
  feature[2] = Variance(green,1);
 }}, 

 //task-4
 new Task() {public void run()
 {
  feature[3] = Kurtosis(green,1);
 }}, 
 //task-5 
 new Task() {public void run()
 {
  feature[4] = Variance(blue,2);
 }},
 //task-6
 new Task() {public void run()
 {
  feature[5] = Kurtosis(blue,2);
 }} 
});
The local array feature[] stores the result of the method execution. So what about ForEach loops? Well they will look a bit different following the next type of structure
Parallel.ForEach(arrayListName, new LoopBody()
{
 public void run(String _objectNameofItemInArray) 
 {
          //code of what must happen to the object
  objectNameofItemInArray.invokeMethod();
 }
}
such that it could look like this example
ArrayList listOfFiles = new ArrayList()

Parallel.ForEach(listOfFiles, new LoopBody()
{

 public void run(ImageObject p) 
 {
  p.process();
 }
}

//In ImageObject class there is a method called process()

public class ImageObject
{
  public ImageObject(){} //Default constructor

  public void process()
  { 
    //ToDo add method logic
  }
}
In this example listOfFiles is an ArrayList of type ImageObject which has a method called process. Threads will be created and multiple ImageObject will then be processed concurrently. There you go a simple way to achieve a basic level of multi threading. Both code snippets are from a project I created to identify duplicate videos and photos.

No comments:

Post a Comment