开发者

Parallel threading in Java

Ok, so I'm trying to find the maximum element of a 2D array. I will have a method that accepts the 2darray as a parameter and finds the maximum. It needs to find the maximum element of each row as a separate thread so that the threads run parrallel, then join each thread, and finding the max of those to get the maximum of the entire 2d array. Now the problem I'm hav开发者_JAVA百科ing is that run() does not return any value...How then am i supposed to access the value that has been modified. for example

public static int maxof2darray(long[][] input){


ArrayList<Thread> threads = new ArrayList<Thread>();
long[]rowArray;
for(int i=0; i<input.length; i++){
rowArray = input[i];
teste r1 = new teste(rowArray,max);
threads.add(new Thread(r1));
}

for ( Thread x : threads )
    {
        x.start();

    }
try {


for ( Thread x : threads)
{
      x.join();
}
}

as you can see it creates an arraylist of thread objects. Then takes each row and calls the run() function that finds the maximum of that row...the problem is run() does not return any value...How then can i possibly access the maximum of that row?


The Future API should do what you need.

A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. Cancellation is performed by the cancel method. Additional methods are provided to determine if the task completed normally or was cancelled. Once a computation has completed, the computation cannot be cancelled. If you would like to use a Future for the sake of cancellability but not provide a usable result, you can declare types of the form Future and return null as a result of the underlying task.


I think this is not proper way for starting and joining the threads. You should use Thread Pool instead.

Following is a sample of code that demonstrates Thread Pool.

ExecutorService workers = Executors.newFixedThreadPool(10); 

for(int i=0; i<input.length; i++) {
    Teste task = new Teste(rowArray,max);
    workers.execute(task);
}

workers.shutdown();
while(!workers.isTerminated()) {
try {
    Thread.sleep(10000);
    } catch (InterruptedException exception) {

    }   
    System.out.println("waiting for submitted task to finish operation");
}

Hope this help.


Unless the array is fairly large it will be faster to do the search in one thread. However say the size is 1000s or more I suggest you use the ExecutionService which is a simple way to manage tasks.

However, the simplest change is to store the result in an AtomicLong, that way your Runnables don't need to return a result.


You can add a new field to your "teste" class that holds the max row. The main thread stops at x.join(), so after that line to can refer to that field and get the max value.

.
.
.
int max=0;
for ( Thread x : threads)
{
      x.join();
      max=x.getMax();
}
.
.
.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜