我遇到了一個奇怪的問題。我試圖使用生產者/消費者模型,請建議如果我在這裏做錯了什麼。 當我使用固定線程爲4的ExecutorService時,我從來沒有得到任何異常和程序運行,但是當我使用ThreadPoolExecutor時,它給了我例外。找不到錯誤!請指教! ExecutorService中的Java ExecutorService和ThreadPoolExecutor
代碼:
ArrayBlockingQueue<BillableList> list =new ArrayBlockingQueue<BillableList>(2);
ThreadFactory threadFactory = Executors.defaultThreadFactory();
ExecutorService threadPool = Executors.newFixedThreadPool(4, threadFactory);
threadPool.execute(new BillingConsu(network,"consumer->"+Thread.currentThread(), list));
threadPool.execute(new BillingConsu(network,"consumer->"+Thread.currentThread(), list));
threadPool.execute(new BillingConsu(network,"consumer->"+Thread.currentThread(), list));
Future producerStatus = threadPool.submit(new BillProdu(this.network,"Producer", list));
producerStatus.get();
threadPool.shutdown();
while (!threadPool.isTerminated()) {
threadPool.shutdown();
threadPool.awaitTermination(10, TimeUnit.SECONDS);
}
代碼的ThreadPoolExecutor的:
ArrayBlockingQueue<BillableList> list =new ArrayBlockingQueue<BillableList>(4);
BlockingQueue<Runnable> worksQueue = new ArrayBlockingQueue<Runnable>(100);
RejectedExecutionHandler executionHandler = new MyRejectedExecutionHandelerImpl();
ThreadFactory threadFactory = Executors.defaultThreadFactory();
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5,5, 10,
TimeUnit.SECONDS, worksQueue,threadFactory, executionHandler);
Future producerStatus = threadPool.submit(new BillProdu(this.network,"Producer", list));
producerStatus.get();
threadPool.execute(new BillingConsu(network,"consumer 1", list));
threadPool.execute(new BillingConsu(network,"consumer 2", list));
threadPool.execute(new BillingConsu(network,"consumer 3", list));
threadPool.execute(new BillingConsu(network,"consumer 4", list));
threadPool.shutdown();
while (!threadPool.isTerminated()) {
threadPool.shutdown();
threadPool.awaitTermination(10, TimeUnit.SECONDS);
}
例外,當我運行的ThreadPoolExecutor:
Exception in thread "pool-1-thread-2" java.lang.ExceptionInInitializerError
at org.apache.axis.utils.Messages.<clinit>(Messages.java:36)
at org.apache.axis.configuration.EngineConfigurationFactoryFinder$1.run (EngineConfigurationFactoryFinder.java:141)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.axis.configuration.EngineConfigurationFactoryFinder.newFactory (EngineConfigurationFactoryFinder.java:113)
at org.apache.axis.configuration.EngineConfigurationFactoryFinder.newFactory (EngineConfigurationFactoryFinder.java:160)
at org.apache.axis.client.Service.getEngineConfiguration(Service.java:813)
at org.apache.axis.client.Service.getAxisClient(Service.java:104)
at org.apache.axis.client.Service.<init>(Service.java:113)
at org.tempuri.OnlineBillingLocator.<init>(OnlineBillingLocator.java:28)
at com.mixem.sdc.sms.StsSmsConnection.<init>(StsSmsConnection.java:40)
at BillingConsu.doStsBilling(BillingConsu.java:202)
at BillingConsu.run(BillingConsu.java:60)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at java.io.FileOutputStream.<init>(FileOutputStream.java:172)
at java.io.FileOutputStream.<init>(FileOutputStream.java:102)
at org.apache.log4j.FileAppender.setFile(FileAppender.java:290)
at LogFileWriter.append(LogFileWriter.java:45)
at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders (AppenderAttachableImpl.java:66)
at org.apache.log4j.Category.callAppenders(Category.java:206)
at org.apache.log4j.Category.forcedLog(Category.java:391)
at org.apache.log4j.Category.log(Category.java:856)
at org.apache.commons.logging.impl.Log4JLogger.debug(Log4JLogger.java:177)
at org.apache.axis.i18n.ProjectResourceBundle.getBundle(ProjectResourceBundle.java:264)
at org.apache.axis.i18n.MessagesConstants.<clinit>(MessagesConstants.java:32)
log4j屬性文件
log4j.rootLogger = DEBUG, fileout
log4j.appender.fileout = LogFileWriter
log4j.appender.fileout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c - %m%n
log4j.appender.fileout.layout = org.apache.log4j.PatternLayout
log4j.appender.fileout.File = /logs/billinglogs.log
LogFileWriter附加代碼
@Override
public void append(LoggingEvent event) {
try {
setFile(appendLevelToFileName((String) MDC.get(ORIG_LOG_FILE_NAME),
event.getLevel().toString()), fileAppend, bufferedIO,bufferSize);
} catch (IOException ie) {
errorHandler.error("Error occured while setting file for the log level "+ event.getLevel(), ie,
ErrorCode.FILE_OPEN_FAILURE);
}
super.append(event);
}
MDC把裏面的代碼LogFileWriter
@Override
public void activateOptions() {
MDC.put(ORIG_LOG_FILE_NAME, fileName);
super.activateOptions();
}
貌似你試圖登錄到空文件。 BillingConsu.java中202是什麼? – BobTheBuilder
就像@whoAmI說的。這似乎是一個log4j配置問題,而不是線程池相關的問題... – fge
@whoAmI它是resp =新的StsSmsConnection()。doRequest(sms);但它是由ExecutorService運行的相同文件!並沒有例外 –