2010-06-18 29 views
1

我應該使用Java以多線程模式處理圖像。我可能有不同數量的圖像,因爲我的線程數量是固定的。我必須使用固定的一組線程處理所有圖像。使用Java以多線程模式處理圖像

我只是卡在如何做到這一點,我看了ThreadExecutor和BlockingQueues等......我仍然不清楚。我正在做的是, - 獲取圖像並將其添加到具有圖像處理器的可運行代碼的LinkedBlockingQueue中。 - 創建一個threadpoolexecutor,其中一個參數是之前的LinkedBlockingQueue。 - 循環遍歷一個for循環,直到隊列大小並執行threadpoolexecutor.execute(linkedblockingqueue.poll)。 - 我看到的只是它僅處理100個圖像,這是在LinkedBlockingQueue大小中傳遞的最小線程大小。

我知道我在某個地方的認識是嚴重錯誤的,我如何處理100個(線程)組中的所有圖像,直到它們全部完成?任何示例或僞代碼將非常有幫助

謝謝! Ĵ

+0

我用一些示例代碼更新了我的帖子。它只是一個快速刺入它並不完美。它完全自成體系。 – 2010-06-18 19:37:58

回答

0

Sun的教程是非常好的,所以我將只張貼鏈接Defining and Starting a Thread

報價: 線程有時稱爲輕量級進程。進程和線程都提供了一個執行環境,但創建一個新線程所需的資源要少於創建新進程的資源。 線程存在於一個進程內 - 每個進程至少有一個線程。線程共享進程的資源,包括內存和打開的文件。這使得溝通有效但可能有問題。

while(que is not empty) 
    start new set of image-processing-thread 
0

你能想到的每一個處理操作是一個「任務」的。將這些任務放在一個隊列中,並讓每個線程在每次完成任務時從該線程中消耗一項任務。

2

這是我寫的一個樣本類。整個事件獨立運行,並從ThreadPool打印每個從1到100的數字。幾乎所有你需要做的就是更新Request類來傳遞你想要的,並重新實現ImageProcessor。

package com.rch.test; 

import java.util.concurrent.BlockingQueue; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.LinkedBlockingQueue; 
import java.util.concurrent.TimeUnit; 

public class Executor 
{ 
    /** 
    * Class to encapsulate a request 
    * 
    * @author romain 
    */ 
    static class Request 
    { 
     String someText; 

     Request(String someText) 
     { 
      this.someText = someText; 
     } 

     public String getSomeText() 
     { 
      return someText; 
     } 
    } 

    /** 
    * Creates a Thread that listens on a queue to process messages 
    * 
    * @author romain 
    */ 
    static class ServerThread implements Runnable 
    { 
     private BlockingQueue<Request> queue = new LinkedBlockingQueue<Request>(); 
     boolean stop = false; 

     /** 
     * Does all the work 
     */ 
     @Override 
     public void run() 
     { 
      ExecutorService pool = Executors.newFixedThreadPool(3); 
      try 
      { 
       while (!stop) 
       { 
        Request req = queue.poll(1000L, TimeUnit.MILLISECONDS); 
        if (req != null) 
        { 
         Runnable runnable = new Executor.ImageProcessor(req); 
         pool.execute(runnable); 
        } 
       } 
      } 
      catch (InterruptedException ie) 
      { 
       System.out.println("Log something here"); 
      } 
      finally 
      { 
       pool.shutdown(); 
      } 
     } 

     /** 
     * Accepts a message on the queue 
     * @param request 
     */ 
     public void accept(Request request) 
     { 
      queue.add(request); 
     } 

     public void stopProcessing() 
     { 
      stop = true; 
     } 
    } 

    /** 
    * class to do the actual work 
    * @author romain 
    */ 
    static class ImageProcessor implements Runnable 
    { 
     String someText; 

     ImageProcessor(Request req) 
     { 
      this.someText = req.getSomeText(); 
     } 

     @Override 
     public void run() 
     { 
      System.out.println(someText); 
      // Process Image here 
     } 
    } 

    /** 
    * Test Harness 
    * @param args 
    */ 
    public static void main(String[] args) 
    { 
     // Initialize 
     ServerThread processor = new ServerThread(); 
     Thread aThread = new Thread(processor); 
     aThread.start(); 

     // Wait for Thread to start 
     try 
     { 
      Thread.sleep(500L); 
     } 
     catch (InterruptedException e1) 
     { 
      e1.printStackTrace(); 
     } 

     for (int i = 0; i < 100; i++) 
     { 
      String text = "" + i; 
      Request aRequest = new Request(text); 
      processor.accept(aRequest); 
     } 

     // Give it enough time to finish 
     try 
     { 
      Thread.sleep(500L); 
     } 
     catch (InterruptedException e1) 
     { 
      e1.printStackTrace(); 
     } 

     // Tell the thread to finish processing 
     processor.stopProcessing(); 

     // Wait for the Thread to complete 
     try 
     { 
      aThread.join(); 
     } 
     catch (InterruptedException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
}