[concurrency-interest] Weird behavior

Tim Peierls tim at peierls.net
Fri Apr 8 22:28:53 EDT 2005


jean.morissette666 at videotron.ca wrote:
> Exception in thread "Thread-2" java.lang.AssertionError: Ouput elements not 
> ordered at execution.operator.Test$3.run(Test.java:99)
> 
> Element outElement = new Element();
> for (int e = 0 ; e < numElements; e++) {
>     inElement = (Element) inputQueue.poll();
>     if (inElement == null) break;
>     outputQueue.offer(inElement);
>     lastOutputTs = inElement.timestamp;
>     if (numProcessed == windowSize) {
>         outElement.timestamp = inElement.timestamp;
>         outputQueue.offer(outElement);
>     } else {
>         numProcessed++;
>     }
> }

Nothing to do with queues or concurrency. You are modifying the timestamp 
of an already enqueued element. The sequence is:

   inElementA(ts=0)
   inElementB(ts=1)
   inElementC(ts=2)
   inElementD(ts=3)
   inElementE(ts=4)
   inElementF(ts=5)
   outElementZ(ts=5) ---+
   inElementG(ts=6)     |
   outElementZ(ts=6) ---+
   inElementH(ts=7)     |
   outElementZ(ts=7) ---+ same Element instance, multiply enqueued
   inElementI(ts=8)     |
   outElementZ(ts=8) ---+
   inElementJ(ts=9)     |
   outElementZ(ts=9) ---+

When windowSize is reached, the timestamp on outElementZ is 9, and the 
order of timestamps in queue is 0, 1, 2, 3, 4, 5, 9, 6, 9, 7, 9, 8, 9, 9.


Probably a typo, but I noticed:

> if (1 % 10 == 0) yield();

This will never call yield(), since 1 % 10 is always 1.

--tim



More information about the Concurrency-interest mailing list