[concurrency-interest] Execution Priority

Fon Francis Tran fon.tran at gmail.com
Thu Jul 7 16:03:28 EDT 2005


Hello,

I tried to test the ThreadPoolExecutor with a simple program but found that 
the runnable jobs
are not executed as FIFO (Using LinkedBlockingQueue) as I would expected 
when the 
corePoolSize is not 1. 

The result and the code are listed below. I would guess the task should be 
executed from
0 to 4 but they seem to be executed at random. Anyone knows why? 

Many thanks,

Fon


Result:

Starting task Task 4 at 04:39:29:179
Ending task Task 4 at 04:39:29:190 after 11 milliseconds
Starting task Task 1 at 04:39:29:200
Ending task Task 1 at 04:39:29:202 after 2 milliseconds
Starting task Task 2 at 04:39:29:207
Ending task Task 2 at 04:39:29:208 after 1 milliseconds
Starting task Task 3 at 04:39:29:211
Ending task Task 3 at 04:39:29:216 after 5 milliseconds
Starting task Task 0 at 04:39:29:219
Ending task Task 0 at 04:39:29:221 after 2 milliseconds
Process terminated with exit code 0
 
Code:

import edu.emory.mathcs.backport.java.util.concurrent.*;
import java.util.*;
import java.text.*;

public class TestConcurrent {
public static void main(String[] args) {
int nTasks = 5;
int fib = 4;

ExecutorServiceAccess esa = new ExecutorServiceAccess();
for (int i = 0; i < nTasks; i++)
esa.excute(new Task2(fib, "Task " + i));
esa.shutdown();
}
}

class ExecutorServiceAccess {
private ExecutorService service;

public ExecutorServiceAccess () {
service = Executors.newFixedThreadPool(5);
}
public void excute(Runnable r) {
service.execute(r);
}
public void shutdown() {
service.shutdown();
}
}

class Task2 implements Runnable {
long n;
String id;

private long doSomething(long n) {
return n*n-n;
}
public Task2(long n, String id) {
this.n = n;
this.id <http://this.id> = id;
}
public void run() {
DateFormat df = new SimpleDateFormat("HH:mm:ss:SSS");
long startTime = System.currentTimeMillis();
System.out.println("Starting task " + id + " at " + df.format(new Date()));
doSomething(n);
long endTime = System.currentTimeMillis();
System.out.println("Ending task " + id + " at " + df.format(new Date())
+ " after " + (endTime - startTime) + " milliseconds");
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20050708/bf7faca7/attachment-0001.htm


More information about the Concurrency-interest mailing list