I am trying to understand something about the newSingleThreadExecutor
- how is said below it executes the tasks sequentially, when no ThreadFactory
mentioned.
public static ExecutorService newSingleThreadExecutor()
Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1)
the returned executor is guaranteed not to be reconfigurable to use additional threads.
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)
Creates an Executor that uses a single worker thread operating off an unbounded queue, and uses the provided ThreadFactory to create a new thread when needed. Unlike the otherwise equivalent newFixedThreadPool(1, threadFactory)
the returned executor is guaranteed not to be reconfigurable to use additional threads.
Parameters:
But given the example below ,where I am using a ThreadFactory :
executor = Executors.newSingleThreadExecutor(getPrioritisedFactory());
`private ThreadFactory getPrioritisedFactory() {
ThreadFactory prioritisedFactory = new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r, recSocketServerThreadID);
t.setPriority(Thread.MAX_PRIORITY);
return t;
}
};
return prioritisedFactory;
}`
doesn't seem to respect the sequentially anymore. Is it because the Threads have the same priority and then is start picking them random ? Is not quite clear from Java help when happens when using factories.
Thanks.
Thanks for the answer I got until now. Here is more code and explanation. What I discovered in the logs in high load conditions (1000 submited threads) that a thread which was submitted first was started after some others submitted later.
The example is simplified, but there is not more code in the MyThread.run method which can delay the logging.
- 1:00:01.100 DEB new thread[id:500]
- 1:02:01.160 DEB new thread[id:900]
- 1:03:01.200 WAR Started thread [thID:900]
- 1:04:02.200 WAR Started thread [thID:500]
private ExecutorService executor;
executor = Executors.newSingleThreadExecutor(getPrioritisedFactory());
....
//Keep adding threads here
MyThread mine=MyThread();
System.out.println(String.format("new thread[id:%d]",
mine.getId()));
executor.execute(mine);
public class MyThread extends Thread
{
@Override
public void run() {
long startTimeMillis = System.currentTimeMillis();
long currentTimeMillis = System.currentTimeMillis();
long delayMillis = currentTimeMillis - this.createTimeMillis;
logger.warn(String.format("Started thread [thID:%d]",
this.getId()));
........
}
}
Every
Executor
there uses aThreadFactory
to create the threads it needs. If you don't provide one explicitly it will take a default one which returns a rather plain thread but with a name.The method without factory will just delegate to the one with.
Executors start out with no thread at all and they will create one when they need one. The amount of threads they may use concurrently depend on their configuration.
If a thread dies for some reason they can create a new one to replace it.
SingleThreadExecutor
is guaranteed to never use more than 1.It is guaranteed that your code is executed sequentially but it is not guaranteed that it will always be the same thread.