Java代码:
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);
final BusinessService service = new BusinessService();
for(int i = 0; i < 10; i++)
{ www.2cto.com
scheduler.scheduleWithFixedDelay(new Runnable(){
@Override
service.handleBusiness();
}
}, i, 5, TimeUnit.MINUTES);
}
这个时候,如果在handleBusiness()方法上加上
Java代码:
synchronized
,其它线程就进不了这个方法,因为这个方法需要耗时5分钟左右,大家帮忙想想有啥好的解决办法,能让所有线程不等待,就可以调用这个方法又保持原子操作.
解决方法:handleBusiness()写成线程安全的就好了。