Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do we have a java queue object or mechanism to handle batch treatment?

Tags:

java

queue

Does Java support any queue object or mechanism to handle batch treatment?

ex: we have a queue(or any desired queue object), some producer push item into the queue one by one, my target is when we have 10 items or more than 10 items in this queue, we can trigger some handler to treat it in one batch.

or it is not triggered automatically, we need to find a way to loop the queue gracefully at the handler side.

do we have typical and high performance object or lib to handle this?

thanks, Emre

like image 561
Emre He Avatar asked Oct 08 '12 08:10

Emre He


1 Answers

Batch processing in Queue could be achievable with wait/notify, something like you would block thread call against the resource upto it is available or not.

public class MyQueue implements Queue<Object>{
        public synchronized List<Object> peek() {
        if(this.list.size()>=10)
                  this.list.wait();
        return Collections.subList(0,10);
    }
        @Override
    public boolean add(Object e) {
        this.list.add(e);
                if(this.list.size()>=10)
                  this.list.notifyAll(); 
        return false;
    }
}

it is not triggered automatically

In that case you can call wait with specified time out.

like image 183
Subhrajyoti Majumder Avatar answered Nov 10 '22 13:11

Subhrajyoti Majumder