我有一个示例java代码,如果作为控制台应用程序运行,我的行为就像我预期的那样(产生一个线程来执行runnable).
奇怪的行为(产生两个线程 - 下面的示例)我看到的是当我使用Apache的prunsrv64.exe将此示例作为服务应用程序运行时.
我在Windows 7机器上测试这个 - 64位.
样本输出:
Thread -28 Current time: 09:50:11 AM
Thread -52 Current time: 09:50:12 AM
Thread -28 Current time: 09:50:21 AM
Thread -52 Current time: 09:50:22 AM
Thread -28 Current time: 09:50:31 AM
Thread -52 Current time: 09:50:32 AM
Run Code Online (Sandbox Code Playgroud)
示例代码:
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExecutorTest{
public void testIt(){
ExecutorService ex = Executors.newSingleThreadExecutor();
ex.execute(new Runnable(){
public void run() {
while(true){
System.out.printf("Thread -" + Thread.currentThread().getId() + " Current time: %tr%n", new Date()); …Run Code Online (Sandbox Code Playgroud) java multithreading executorservice java-7 scheduledexecutorservice