Take the 2-minute tour ×

One of the things a core java server i'm running does is process two type of tasks (T1 and T2) . Originally these tasks were being processed from the same queue and thread. At times the queue contains about 1000 T1 tasks which cause delays for the next T2 task. So i decided to change the architecture and process each task in a separate thread and separate queue. But it looks like the JVM will still process the ~1000 T1 tasks (about .6 seconds) before switching to the other thread and processing T2 tasks.

I'm running on a quad core machine running "Red Hat Enterprise Linux Server release 6.1 (Santiago)".

I'm assuming that the two threads are being run on the same CPU.

Is there any way to facilitate the OS or JVM to switch more often? Also is there a way to see which thread is being processed by which processor?

I've verified that both threads are running as separate threads using a debugger.

One thread is created using


And tasks are submitted using

executor.submit(new Task2(..));

The other thread is legacy code that manages it's own queue:

private final BlockingQueue<...> workQueue = new LinkedBlockingQueue<...>();
public void submit(Task1 task) {


    public void run() {
    while (alive){
            Task1 task = workQueue.take();
share|improve this question
maybe there's a problem with your code, which you didn't post? –  Marko Topolnik Nov 27 '12 at 15:46
Are you 100% positive that the 2 tasks run in different threads? How do you determine that T2 only starts after all 1000 T1 have been processed? –  assylias Nov 27 '12 at 15:47
Are you calling run on a Runnable rather than holding the Runnable in a Thread and calling start on that ? Just a hunch :-) Please post your code –  Brian Agnew Nov 27 '12 at 15:49
Please post some code. That would help the community answer your question. :) –  Jose Chavez Nov 27 '12 at 15:56
I'm 100% positive that 2 tasks are running in different threads. I conclude that t2 only starts after 1000 t1 based on logs that show the time each tasks is processed. –  richs Nov 27 '12 at 16:50

3 Answers 3

I agree with the comments suggesting that there may be a program problem preventing parallelism. However, there are two things you could do to suggest more switching if that is not the case:

  1. Reduce the priority of the T1 thread.
  2. Insert a yield call in the T1 thread at the end of each T1 task.
share|improve this answer
Beware of java thread priorities, they are pretty much useless due to the way the JVM tries to map to OS thread priorities. –  lynks Nov 27 '12 at 15:54

From your current description, this is the correct answer: You need to invoke thread.start() rather than thread.run()

If not you need to post code :)

share|improve this answer
up vote 0 down vote accepted

After more analysis I found the delay was caused earlier (before submitting) by another thread that the two tasks share.

share|improve this answer

Your Answer


