使用Java以multithreading模式处理图像
我应该使用Java以multithreading模式处理图像。 当我的线程数被修复时,我可能有不同数量的图像。 我必须使用固定的线程集处理所有图像。
我只是坚持如何做到这一点,我看看ThreadExecutor和BlockingQueues等…我仍然不清楚。 我正在做的是, – 获取图像并将它们添加到LinkedBlockingQueue中,LinkedBlockingQueue具有图像处理器的可运行代码。 – 创建一个threadpoolexecutor,其中一个争论是之前的LinkedBlockingQueue。 – 通过for循环迭代直到队列大小并执行threadpoolexecutor.execute(linkedblockingqueue.poll)。 – 我只看到它只处理100个图像,这是在LinkedBlockingQueue大小中传递的最小线程大小。
我知道我在某处理解我是非常错误的,如何处理100(线程)组中的所有图像,直到它们全部完成? 任何示例或伪代码都非常有用
谢谢! Ĵ
这是我写的一个示例类。 整个程序独立运行,并从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 queue = new LinkedBlockingQueue (); 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(); } } }
您可以将每个处理操作视为“任务”。 将这些任务放在一个队列中,并让每个线程在每次完成任务时都从该线程中消耗任务。
Sun的教程非常好,所以我将发布定义和开始一个主题的链接
Quote:线程有时被称为轻量级进程。 进程和线程都提供执行环境,但创建新线程所需的资源比创建新进程要少。 线程存在于进程中 – 每个进程至少有一个进程。 线程共享进程的资源,包括内存和打开文件。 这使得有效但可能有问题的通信成为可能。
while(que is not empty) start new set of image-processing-thread
- 创建名为’org.springframework.security.filterChains’Spring + Hibernate + UserDetailService的bean时出错
- java.util.Stack的Iterator中是否有错误?
- 如何更改jdesktoppane默认背景图片?
- 将.swf文件嵌入我的Jframe
- JSP Document / JSPX:什么决定了输出中如何删除制表符/空格/换行符?
- 我们可以在Java Swing中使用GTK + 2.0按钮样式吗?
- 在工作单元克隆中遇到JPA Null或零主键
- Java中的多重inheritance设计问题
- 如何在spring mvc拦截器中validation会话