Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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_Multithreading_Executorservice - Fatal编程技术网

java多线程,为每个线程创建对象

java多线程,为每个线程创建对象,java,multithreading,executorservice,Java,Multithreading,Executorservice,我有两个类CallisPubscriberDump(这个类从数据库中读取进程id的总数,对于每个进程id,它调用ISPSubscriberDump类来创建一个文件)和ISPSubscriberDump(用于创建ISP订户转储文件,该文件被发送到网络以供提供)。 我使用Executor服务创建多个线程,并在其构造函数中将进程id传递给ISPSubscriberDump,但在这种方法中,我必须创建与运行线程数量相同的对象。这个过程运作良好。 由于我必须为每个进程Id运行一个线程,是否有其他方法可以只

我有两个类CallisPubscriberDump(这个类从数据库中读取进程id的总数,对于每个进程id,它调用ISPSubscriberDump类来创建一个文件)和ISPSubscriberDump(用于创建ISP订户转储文件,该文件被发送到网络以供提供)。 我使用Executor服务创建多个线程,并在其构造函数中将进程id传递给ISPSubscriberDump,但在这种方法中,我必须创建与运行线程数量相同的对象。这个过程运作良好。 由于我必须为每个进程Id运行一个线程,是否有其他方法可以只创建单个对象并装箱多个对象

public class CallISPSubscriberDump
{

    public  void createFile()
    {

        List<Integer> totalId = new ArrayList<Integer>();
        List<String> dataFlag = new ArrayList<String>();


        //Reading process Id and dataFlag from database and populating in list
        try
        {
            if (totalId.size() == 0)
            {
                throw new Exception("No process id found for ISP Dump");
            }
            else
            {
                // MAX_THRAED is max limit of threads
                int maxthred = totalId.size() < Integer.parseInt(logGererator.getProperty("MAX_THRAED")) ? totalId.size() :
                                                Integer.parseInt(logGererator.getProperty("MAX_THRAED"));

                ExecutorService executor = Executors.newFixedThreadPool(maxthred);

                for (int cnt = 0; cnt < totalId.size(); cnt++)
                {
                    //For a particular thread assigning a particular process I create N object of ISPSubscriberDump for N thread and assign process Id in its constructor
                    executor.execute(new ISPSubscriberDump(totalId.get(cnt),dataFlag.get(cnt)));
                }

                executor.shutdown();

                while (!executor.isTerminated())
                {
                }
                System.out.println("Finished all threads");


            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
TL;博士

“是否有其他方法可以仅创建单个对象并装箱多个对象?”


也许您可以使用singleton/factory类。

您可以使用队列,让线程从中选择对象任务。即使使用队列,我也无法解决它。请您详细解释一下
public class ISPSubscriberDump implements Runnable 
{

    private int processId;
    private String dataFlag;

    public ISPSubscriberDump(int processId,String dataFlag){
        this.processId=processId;
        this.dataFlag=dataFlag;
    }

    public void run()
    {
        // File Creation 
        createFile();
    }

    createFile()
    {
    int currentProcessId=processId;
    String currentDataFlag= dataFlag;
    // File Creation and provising happened here using currentProcessId and currentDataFlag
    }

    }