You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by slump <sl...@21cn.com> on 2008/09/04 11:29:22 UTC

race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor

I think I found a race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor

I write a textline echo server using MINA

I add a UnorderedThreadPoolExecutor in server's filter:

 acceptor.getFilterChain().addLast("executor",
            new ExecutorFilter(new UnorderedThreadPoolExecutor(10)));

 and in messageReceived, I add sleep(3000) before echo the line received

when I send 10 lines from client , I think the server should use 10 threads to process 10 message concurrently
but the server only use 2 thread or 4 thread , and the remain message must wait until sleep complete

I changed executor to  ThreadPoolExecutor in JDK, it will use 10 thread

I checked and trace the code of  UnorderedThreadPoolExecutor, find the reason:
   private void addWorkerIfNecessary() {
        if (idleWorkers.get() == 0) {
            synchronized (workers) {
                if (workers.isEmpty() || idleWorkers.get() == 0) {
                    addWorker();
                }
            }
        }
    }

there may be some race condition:
1 thread A call executor's execute to sunmit one task
2 executor find there are no idle worker, so create a new thread and start it, now idleWorkers is 1 
3 before new worker thread fetch task and decrement the idleWorkers, the thread A submit more task
4 the executor find idleWorkers is 1 , so don't create new thread
5 the new worker thread fetch task and decrement the idleWorkers, now idleWorkers is 0 ,but there are some task left in queue without worker

MAYBE the   idleWorkers.get() == 0 should be change to idleWorkers.get() < getQueue().size() ?

And in OrderedThreadPoolExecutor  , have the same race condition.


And I have one more question: 
Why not just use  ThreadPoolExecutor in JDK instead of  UnorderedThreadPoolExecutor? 
does UnorderedThreadPoolExecutor have some special requirement which  ThreadPoolExecutor can't fulfil?
       



dingli
2008-09-04

Re: Re: race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor

Posted by slump <sl...@21cn.com>.
>>At worst, you will have a task waiting for a thread in the queue, until another task finish. This is a
>>best effort system, where you create a new thread if there is none
>>remaining, which does not mean each incoming task will be affected to
>>this new thread.

yes, the new task will be executed at last
but it seems the concurrent worker threads is less than maximumPoolSize   which is spcified in constructor 
I think when there are tasks in queue and worker threads <max-thread  
new worker threads should be created just like in JDK's ThreadPoolExecutor 

But if in design goal , maximumPoolSize don't mean create new thread EVERY time  in this situation, it is OK.

Thank you for the reply.




slump
2008-10-06



发件人: Emmanuel Lecharny
发送时间: 2008-09-29 02:27:39
收件人: dev@mina.apache.org
抄送: 
主题: Re: race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor

slump wrote:
> I think I found a race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor
>  <snip/ >
> And in OrderedThreadPoolExecutor  , have the same race condition.
>   
I don't think there is a race condition. At worst, you will have a task
waiting for a thread in the queue, until another task finish. This is a
best effort system, where you create a new thread if there is none
remaining, which does not mean each incoming task will be affected to
this new thread.
> And I have one more question: 
> Why not just use  ThreadPoolExecutor in JDK instead of  UnorderedThreadPoolExecutor? 
> does UnorderedThreadPoolExecutor have some special requirement which  ThreadPoolExecutor can't fulfil?
>   
The class javadoc is pretty clear about the diffeence between the
UnorderedThreadPoolExecutor and the JDK ThreadPollExecutor class. And
you can verify that the MINA class extends the ThreadPollExecutor class.
It just add extra semantic.

Hope it helps.
>   


-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org

Re: race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor

Posted by Emmanuel Lecharny <el...@gmail.com>.
slump wrote:
> I think I found a race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor
> <snip/>
> And in OrderedThreadPoolExecutor  , have the same race condition.
>   
I don't think there is a race condition. At worst, you will have a task
waiting for a thread in the queue, until another task finish. This is a
best effort system, where you create a new thread if there is none
remaining, which does not mean each incoming task will be affected to
this new thread.
> And I have one more question: 
> Why not just use  ThreadPoolExecutor in JDK instead of  UnorderedThreadPoolExecutor? 
> does UnorderedThreadPoolExecutor have some special requirement which  ThreadPoolExecutor can't fulfil?
>   
The class javadoc is pretty clear about the diffeence between the
UnorderedThreadPoolExecutor and the JDK ThreadPollExecutor class. And
you can verify that the MINA class extends the ThreadPollExecutor class.
It just add extra semantic.

Hope it helps.
>   


-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor

Posted by Emmanuel Lecharny <el...@gmail.com>.
slump wrote:
> Anyone can answer this?
> If I am wrong, please tell me 
>   
No time to check atm. Can you create a JIRA with your first message ? We
will process it later (mails get lost, when JIRA stay in our way...)

Thanks !
> Thanks
>
>
>
>
> slump
> 2008-09-28
>
>
>
> 发件人: slump
> 发送时间: 2008-09-04 17:30:35
> 收件人: dev
> 抄送: 
> 主题: race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor
>
>
> I think I found a race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor
>
> I write a textline echo server using MINA
>
> I add a UnorderedThreadPoolExecutor in server's filter:
>
>  acceptor.getFilterChain().addLast("executor",
>             new ExecutorFilter(new UnorderedThreadPoolExecutor(10)));
>
>  and in messageReceived, I add sleep(3000) before echo the line received
>
> when I send 10 lines from client , I think the server should use 10 threads to process 10 message concurrently
> but the server only use 2 thread or 4 thread , and the remain message must wait until sleep complete
>
> I changed executor to  ThreadPoolExecutor in JDK, it will use 10 thread
>
> I checked and trace the code of  UnorderedThreadPoolExecutor, find the reason:
>    private void addWorkerIfNecessary() {
>         if (idleWorkers.get() == 0) {
>             synchronized (workers) {
>                 if (workers.isEmpty() || idleWorkers.get() == 0) {
>                     addWorker();
>                 }
>             }
>         }
>     }
>
> there may be some race condition:
> 1 thread A call executor's execute to sunmit one task
> 2 executor find there are no idle worker, so create a new thread and start it, now idleWorkers is 1 
> 3 before new worker thread fetch task and decrement the idleWorkers, the thread A submit more task
> 4 the executor find idleWorkers is 1 , so don't create new thread
> 5 the new worker thread fetch task and decrement the idleWorkers, now idleWorkers is 0 ,but there are some task left in queue without worker
>
> MAYBE the   idleWorkers.get() == 0 should be change to idleWorkers.get()  < getQueue().size() ?
>
> And in OrderedThreadPoolExecutor  , have the same race condition.
>
>
> And I have one more question: 
> Why not just use  ThreadPoolExecutor in JDK instead of  UnorderedThreadPoolExecutor? 
> does UnorderedThreadPoolExecutor have some special requirement which  ThreadPoolExecutor can't fulfil?
>        
>
>
>
> dingli
> 2008-09-04
>   


-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor

Posted by slump <sl...@21cn.com>.
Anyone can answer this?
If I am wrong, please tell me 

Thanks




slump
2008-09-28



发件人: slump
发送时间: 2008-09-04 17:30:35
收件人: dev
抄送: 
主题: race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor


I think I found a race condition in OrderedThreadPoolExecutor/UnorderedThreadPoolExecutor

I write a textline echo server using MINA

I add a UnorderedThreadPoolExecutor in server's filter:

 acceptor.getFilterChain().addLast("executor",
            new ExecutorFilter(new UnorderedThreadPoolExecutor(10)));

 and in messageReceived, I add sleep(3000) before echo the line received

when I send 10 lines from client , I think the server should use 10 threads to process 10 message concurrently
but the server only use 2 thread or 4 thread , and the remain message must wait until sleep complete

I changed executor to  ThreadPoolExecutor in JDK, it will use 10 thread

I checked and trace the code of  UnorderedThreadPoolExecutor, find the reason:
   private void addWorkerIfNecessary() {
        if (idleWorkers.get() == 0) {
            synchronized (workers) {
                if (workers.isEmpty() || idleWorkers.get() == 0) {
                    addWorker();
                }
            }
        }
    }

there may be some race condition:
1 thread A call executor's execute to sunmit one task
2 executor find there are no idle worker, so create a new thread and start it, now idleWorkers is 1 
3 before new worker thread fetch task and decrement the idleWorkers, the thread A submit more task
4 the executor find idleWorkers is 1 , so don't create new thread
5 the new worker thread fetch task and decrement the idleWorkers, now idleWorkers is 0 ,but there are some task left in queue without worker

MAYBE the   idleWorkers.get() == 0 should be change to idleWorkers.get()  < getQueue().size() ?

And in OrderedThreadPoolExecutor  , have the same race condition.


And I have one more question: 
Why not just use  ThreadPoolExecutor in JDK instead of  UnorderedThreadPoolExecutor? 
does UnorderedThreadPoolExecutor have some special requirement which  ThreadPoolExecutor can't fulfil?
       



dingli
2008-09-04