Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java在多线程模式下进行图像处理_Java_Image Processing_Concurrency - Fatal编程技术网

使用Java在多线程模式下进行图像处理

使用Java在多线程模式下进行图像处理,java,image-processing,concurrency,Java,Image Processing,Concurrency,我应该使用Java以多线程模式处理图像。我可能有不同数量的图像,因为我的线程数量是固定的。我必须使用固定的线程集处理所有图像 我只是被困在如何做到这一点,我看了线程执行器和阻塞队列等…我仍然不清楚。我现在做的是, -获取图像并将其添加到LinkedBlockingQueue中,该队列具有图像处理器的可运行代码。 -创建一个threadpoolexecutor,其中一个参数是前面的LinkedBlockingQueue。 -迭代for循环直到队列大小,然后执行threadpoolexecutor.

我应该使用Java以多线程模式处理图像。我可能有不同数量的图像,因为我的线程数量是固定的。我必须使用固定的线程集处理所有图像

我只是被困在如何做到这一点,我看了线程执行器和阻塞队列等…我仍然不清楚。我现在做的是, -获取图像并将其添加到LinkedBlockingQueue中,该队列具有图像处理器的可运行代码。 -创建一个threadpoolexecutor,其中一个参数是前面的LinkedBlockingQueue。 -迭代for循环直到队列大小,然后执行threadpoolexecutor.execute(linkedblockingqueue.poll)。 -我只看到它只处理100个图像,这是LinkedBlockingQueue size中传递的最小线程大小

我发现我在某些地方的理解是严重错误的,我如何处理100组(线程)中的所有图像,直到它们全部完成?任何示例或伪代码都将非常有用

谢谢!
J

Sun的教程非常好,所以我只发布链接

引述: 线程有时被称为轻量级进程。进程和线程都提供了一个执行环境,但是创建一个新线程比创建一个新进程需要更少的资源。 线程存在于进程中-每个进程至少有一个线程。线程共享进程的资源,包括内存和打开的文件。这有助于实现高效但可能存在问题的沟通


您可以将每个处理操作视为一个“任务”。将这些任务放在一个队列中,让每个线程在每次完成任务时都使用该线程中的一个任务。

下面是我编写的示例类。整个程序独立运行,并从一个线程池中打印一个从1到100的数字。几乎所有您需要做的就是更新请求类以传入您想要的内容并重新实现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();
        }
    }
}
package com.rch.test;
导入java.util.concurrent.BlockingQueue;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.LinkedBlockingQueue;
导入java.util.concurrent.TimeUnit;
公共类执行者
{
/**
*类来封装请求
* 
*@作者罗曼
*/
静态类请求
{
字符串someText;
请求(字符串someText)
{
this.someText=someText;
}
公共字符串getSomeText()
{
返回一些文本;
}
}
/**
*创建侦听队列以处理消息的线程
* 
*@作者罗曼
*/
静态类ServerThread实现可运行
{
private BlockingQueue=新建LinkedBlockingQueue();
布尔停止=假;
/**
*做所有的工作
*/
@凌驾
公开募捐
{
ExecutorService池=Executors.newFixedThreadPool(3);
尝试
{
当(!停止)
{
请求请求=队列轮询(1000L,时间单位为毫秒);
如果(请求!=null)
{
Runnable Runnable=新的执行器.ImageProcessor(req);
pool.execute(可运行);
}
}
}
捕获(中断异常ie)
{
System.out.println(“在这里记录一些东西”);
}
最后
{
pool.shutdown();
}
}
/**
*接受队列上的消息
*@param请求
*/
公共无效接受(请求)
{
添加(请求);
}
公共无效停止处理()
{
停止=真;
}
}
/**
*上课做实际工作
*@作者罗曼
*/
静态类ImageProcessor实现可运行
{
字符串someText;
图像处理器(请求请求)
{
this.someText=req.getSomeText();
}
@凌驾
公开募捐
{
System.out.println(someText);
//在此处理图像
}
}
/**
*测试线束
*@param args
*/
公共静态void main(字符串[]args)
{
//初始化
ServerThread处理器=新的ServerThread();
Thread aThread=新线程(处理器);
aThread.start();
//等待线程启动
尝试
{
线程。睡眠(500L);
}
捕捉(中断异常e1)
{
e1.printStackTrace();
}
对于(int i=0;i<100;i++)
{
字符串text=”“+i;
请求请求=新请求(文本);
处理器。接受(请求);
}
//给它足够的时间完成
尝试
{
线程。睡眠(500L);
}
捕捉(中断异常e1)
{
e1.printStackTrace();
}
//告诉线程完成处理
processor.stopProcessing();
//等待线程完成
尝试
{
aThread.join();
}
捕捉(中断异常e)
{
e、 printStackTrace();
}
}
}

我用一些示例代码更新了我的帖子。这不是完美的,只是一个快速的尝试。它完全独立运行。
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();
        }
    }
}