Java 通过一个线程获取作业,但通过多个线程并行执行作业
我正在从一家DB公司找工作。我希望从数据库获取记录是单线程的。一旦我有了所有的作业,我想让它们以并行的线程数执行。正如您所说,您正在使用Spring,我建议您查找 然后,您可以让查询DB的线程将作业(包装为Java 通过一个线程获取作业,但通过多个线程并行执行作业,java,multithreading,Java,Multithreading,我正在从一家DB公司找工作。我希望从数据库获取记录是单线程的。一旦我有了所有的作业,我想让它们以并行的线程数执行。正如您所说,您正在使用Spring,我建议您查找 然后,您可以让查询DB的线程将作业(包装为Callable或Runnable)提交给ThreadPoolTaskExecutor 首先从数据库中获取要处理的记录 我正在将记录与客户分组 我正在将每个客户的处理传递到线程池 ..... @Autowired ThreadPoolTaskExecutor taskExecutor; @A
Callable
或Runnable
)提交给ThreadPoolTaskExecutor
.....
@Autowired
ThreadPoolTaskExecutor taskExecutor;
@Autowired
private ApplicationContext applicationContext;
@Transactional
public void processPendingPayments() {
logger.debug("ProcessPendingPayments.processPendingPayments begins");
logger.info("startTime::" + System.currentTimeMillis());
List<PaymentRequestEntity> recordsPendingForPayment = getRecordsPendingForPayment();
// Group Requests with Customers
Map<String, List<PaymentRequestEntity>> customerPaymentRequestsMap = getPendingRecordsGroupedByCustomer(
recordsPendingForPayment);
// process Payment requests for customers
for (String customerId : customerPaymentRequestsMap.keySet()) {
CustomerPaymentRequestProcessor customerPaymentRequestProcessor = (CustomerPaymentRequestProcessor) applicationContext
.getBean("customerPaymentRequestProcessor");
customerPaymentRequestProcessor.setStrCustomerId(customerId);
customerPaymentRequestProcessor.setCustomerPaymentRequestsMap(customerPaymentRequestsMap);
taskExecutor.execute(customerPaymentRequestProcessor);
}
logger.info("endTime::" + System.currentTimeMillis());
logger.debug("ProcessPendingPayments.processPendingPayments ends");
}
........
。。。。。
@自动连线
线程池任务执行器任务执行器;
@自动连线
私有应用程序上下文应用程序上下文;
@交易的
public void processPendingPayments(){
调试(“ProcessPendingPayments.ProcessPendingPayments开始”);
logger.info(“startTime::”+System.currentTimeMillis());
List recordsPendingForPayment=getRecordsPendingForPayment();
//与客户分组请求
映射customerPaymentRequestsMap=getPendingRecordsGroupedByCustomer(
付款记录);
//处理客户的付款请求
for(字符串customerId:customerPaymentRequestsMap.keySet()){
CustomerPaymentRequestProcessor CustomerPaymentRequestProcessor=(CustomerPaymentRequestProcessor)应用上下文
.getBean(“customerPaymentRequestProcessor”);
customerPaymentRequestProcessor.setStrCustomerId(customerId);
customerPaymentRequestProcessor.setCustomerPaymentRequestsMap(customerPaymentRequestsMap);
taskExecutor.execute(customerPaymentRequestProcessor);
}
logger.info(“endTime::”+System.currentTimeMillis());
调试(“ProcessPendingPayments.ProcessPendingPayments结束”);
}
........
下面是处理每个客户记录的Runnable类的代码
@Component
@Scope("prototype")
public class CustomerPaymentRequestProcessor implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(CustomerPaymentRequestProcessor.class);
private String strCustomerId;
private Map<String, List<PaymentRequestEntity>> customerPaymentRequestsMap;
private final String REQUEST_TYPE_BALANCE = "Balance";
@Autowired
private BalanceServiceImpl balanceServiceImpl;
@Autowired
PendingPaymentsSequenceProcessor pendingPaymentsSequenceProcessor;
@Autowired
CustomerAuditServiceImpl customerAuditServiceImpl;
public CustomerPaymentRequestProcessor() {
}
public CustomerPaymentRequestProcessor(String strCustomerId,
Map<String, List<PaymentRequestEntity>> customerPaymentRequestsMap) {
super();
this.strCustomerId = strCustomerId;
this.customerPaymentRequestsMap = customerPaymentRequestsMap;
}
@Override
public void run() {
logger.debug("CustomerPaymentRequestProcessor.run starts");
List<PaymentRequestEntity> paymentRequests = getCustomerPaymentRequestsMap().get(getStrCustomerId());
for (PaymentRequestEntity paymentRequest : paymentRequests) {
CustomerBalanceResponse customerBalanceResponse = processPaymentRequest(paymentRequest);
if (!customerBalanceResponse.isCustomerHasBalance()
&& REQUEST_TYPE_BALANCE.equals(paymentRequest.getRequestType())) {
break;
}
}
logger.debug("CustomerPaymentRequestProcessor.run ends");
}
....
....
}
@组件
@范围(“原型”)
公共类CustomerPaymentRequestProcessor实现Runnable{
私有静态最终记录器Logger=LoggerFactory.getLogger(CustomerPaymentRequestProcessor.class);
私有字符串strCustomerId;
私有地图customerPaymentRequestsMap;
私有最终字符串请求\u类型\u BALANCE=“BALANCE”;
@自动连线
私人余额服务impl余额服务impl;
@自动连线
PendingPaymentsSequenceProcessor PendingPaymentsSequenceProcessor;
@自动连线
CustomerAuditServiceImpl CustomerAuditServiceImpl;
公共CustomerPaymentRequestProcessor(){
}
公共CustomerPaymentRequestProcessor(字符串strustomeId,
映射customerPaymentRequestsMap){
超级();
this.strustomerid=strustomerid;
this.customerPaymentRequestsMap=customerPaymentRequestsMap;
}
@凌驾
公开募捐{
debug(“CustomerPaymentRequestProcessor.run启动”);
List paymentRequests=getCustomerPaymentRequestsMap().get(GetStrcustomeId());
for(PaymentRequestEntity paymentRequest:paymentRequests){
CustomerBalanceResponse CustomerBalanceResponse=processPaymentRequest(paymentRequest);
如果(!customerBalanceResponse.isCustomerHasBalance())
&&请求\类型\余额.equals(paymentRequest.getRequestType()){
打破
}
}
debug(“CustomerPaymentRequestProcessor.run结束”);
}
....
....
}
你的问题到底是什么?到目前为止,您尝试了什么?我正在使用Spring调度程序来实现这一点。但是所有这些我都是用一个线程来完成的。但是现在我打算让它成为多线程的。很抱歉,你的问题并没有真正说明你的问题。但为了有所帮助,请尝试设置一个由查询数据库的线程填充的
BlockingQueue
,并从队列中轮询线程池,直到队列为空。@JeremyGrand:谢谢我的尝试。但有没有比使用阻塞队列更好的方法呢。