我有两个类CallISPSubscriberDump(此类从数据库中读取总进程ID,对于每个进程id,它调用ISPSubscriberDump类来创建文件)和ISPSubscriberDump(用于创建ISP用户转储文件,发送到网络进行提供) )。 我正在使用Executor Service创建多个线程并在其构造函数中将进程ID传递给ISPSubscriberDump,但在这种方法中,我必须创建尽可能多的线程正在运行的对象。这个过程很好。 因为我必须为每个进程ID运行一个线程,有没有其他方法可以创建只有单个对象和crate多个对象?
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();
}
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
}
}
答案 0 :(得分:-1)
TL; DR
&#34;有没有其他方法可以创建单个对象并创建多个对象?&#34;
也许你可以使用单件/工厂类。