我的任务每1分钟运行一次。此任务包含一些处理某些数据的方法。 Process1读取状态1中的数据(一个或多个),并在此过程结束时将状态更新为2. Process2读取状态2中的数据,依此类推。所以现在我想使用taskExecutor
(org.springframework.core.task.SimpleAsyncTaskExecutor
)改进此处理以并行运行:
public void process1() {
List<Object> objects = someDao.readDataWithStatus("1");
if (objects == null || objects.isEmpty()) {
return;
}
for (final Object object : objects) {
if (BooleanUtils.isTrue(isParalelProcess())) {
taskExecutor.execute(new Runnable() {
@Override
public void run() {
process(object);
}
});
} else {
process(object);
}
}
}
假设我们有一个status1对象,我们想要并行处理。每隔一分钟运行的任务开始处理。 Process1读取状态为1的数据,将其放入taskExecutor并转到下一个方法。这个方法没有找到状态2的对象,所以这里没什么可做的。一分钟后,process2读取状态为2的对象,依此类推。处理对象需要从1秒到几分钟。正如您所看到的那样,流程显着减慢,仅需几秒钟。有没有选项如何改善这种处理?
答案 0 :(得分:0)
我花了很少的时间。它可能对你有所帮助。
public class SampleTest
{
public static void main(String[] args)
{
int noOfThreads = 100;
int maxThreadinQue = 100;
LinkedBlockingQueue<Runnable> processOneworkQueue = new LinkedBlockingQueue<Runnable>();
ThreadPoolExecutor processOneThreadPoolExecutor = new ThreadPoolExecutor(noOfThreads, noOfThreads, 0L,
TimeUnit.SECONDS,
processOneworkQueue, new ThreadFactory()
{
private AtomicInteger itsCounter = new AtomicInteger();
public Thread newThread(Runnable theRunnable)
{
Thread aThread = new Thread(theRunnable, "theThreadName" + "#" + itsCounter.getAndIncrement());
aThread.setDaemon(true);
return aThread;
}
});
LinkedBlockingQueue<Runnable> processTwoworkQueue = new LinkedBlockingQueue<Runnable>();
ThreadPoolExecutor processTwoThreadPoolExecutor = new ThreadPoolExecutor(noOfThreads, noOfThreads, 0L,
TimeUnit.SECONDS,
processTwoworkQueue, new ThreadFactory()
{
private AtomicInteger itsCounter = new AtomicInteger();
public Thread newThread(Runnable theRunnable)
{
Thread aThread = new Thread(theRunnable, "theThreadName" + "#" + itsCounter.getAndIncrement());
aThread.setDaemon(true);
return aThread;
}
});
ArrayList<SampleObject> sampleObjects = new ArrayList<SampleObject>();
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
sampleObjects.add(new SampleObject());
while (true)
{
Iterator<SampleObject> it = sampleObjects.iterator();
while (it.hasNext())
{
final SampleObject sampleObject = it.next();
if (sampleObject.getStatus() == 0)
{
if (processOneworkQueue.size() < maxThreadinQue)
{
processOneThreadPoolExecutor.submit(new Runnable()
{
@Override
public void run()
{
process1(sampleObject);
}
});
}
}
else if (sampleObject.getStatus() == 1)
{
if (processTwoworkQueue.size() < maxThreadinQue)
{
processTwoThreadPoolExecutor.submit(new Runnable()
{
@Override
public void run()
{
process2(sampleObject);
}
});
}
}
else if (sampleObject.getStatus() == 2)
{
it.remove();
}
}
}
}
static void process1(SampleObject sampleObject)
{
System.out.println("process1");
sampleObject.setStatus(1);
}
static void process2(SampleObject sampleObject)
{
System.out.println("process2");
sampleObject.setStatus(2);
}
}
class SampleObject
{
int status=0;
public int getStatus()
{
return status;
}
public void setStatus(int status)
{
this.status = status;
}
}