我正在IBM自由服务器上工作,我需要在其中实现5个线程的并发性。我已经浏览了IBM官方网站上的一些链接。但是无法理解应该如何配置server.xml,以便每当我将Java应用程序部署到自由服务器5上时,线程就会同时运行Java组件。任何人都可以用在这个问题上苦苦挣扎的可靠例子来帮助我。以下是我的代码库,
我通过以下更改修改了server.xml,
<featureManager>
<feature>concurrent-1.0</feature>
</featureManager>
<managedScheduledExecutorService jndiName="concurrent/scheduledExecutor1">
<contextService jndiName="concurrent/threadContextSvc2"/>
<concurrencyPolicy max="2"/>
</managedScheduledExecutorService>
web.xml
<resource-env-ref>
<resource-env-ref-name>concurrent/scheduledExecutor1</resource-env-ref-name>
<resource-env-ref-type>javax.enterprise.concurrent.ManagedScheduledExecutorService</resource-env-ref-type>
</resource-env-ref>
<resource-env-ref>
<resource-env-ref-name>concurrent/threadContextSvc2</resource-env-ref-name>
<resource-env-ref-type>javax.enterprise.concurrent.ContextService</resource-env-ref-type>
</resource-env-ref>
main()方法
InitialContext initialContext = new InitialContext();
executor = (ManagedScheduledExecutorService) initialContext.lookup("concurrent/scheduledExecutor1");
executor.submit(new OrderProcessCI());
run()方法
public void run() {
System.out.println(" Thread name :: "+Thread.currentThread().getName()+" Thread id :: "+Thread.currentThread().getId());
}
在上面的代码中似乎只有一个线程正在运行。当我尝试在run方法中达到断点时,控件只会出现一次。 Sysout仅打印一次。我怎样才能使至少5个线程。我还检查了ThreadPool的概念,但是如何自由实现呢。有人可以帮我举任何例子。
您只看到一个线程,因为您只向执行者executor.submit(new OrderProcessCI());
提交了一个任务,如果要执行其中5个任务,就需要以某种方式向执行者提交5个实例。您是否有意使用ManagedScheduleExecutorService,因为您需要安排重复发生的工作或将来的工作?如果只需要运行任务,请改为使用ManangedExecutorService。由于您似乎没有更改线程上下文,因此从config中省略它,并让执行程序使用其默认值。给定您说的是“ 5个线程”,我假设您恰好想要那么多线程(让执行者确定需要多少线程的首选方法),您需要在server.xml <concurrencyPolicy max="5" maxPolicy="strict"/>