multithreading - Java POJO: strategies for handling a queue of request objects to a server -
right i'm torn in deciding best way of handling request objects send server. in other words, have tracking request objects things such impression , click tracking within app. simple requests low payloads. there places in app said objects need tracked appear concurrently next each other (at 3 concurrent objects have track), every time said objects visible example, have create tracking request object each of them.
now know can create singleton queue thread adds objects vector , thread either processes them in main loop or calls wait on queue until have objects process. while sounds clear cut solution, queue can accumulate dozens, can cumbersome @ times, since it's making 1 connection each request, won't run concurrently.
what had in mind create thread pool allow me create 2 concurrent connections via semaphore , process thread objects contain tracking event requests. in other words, wanted create function create new thread object , add vector, in thread pool iterate through set of threads , process them 2 @ time. know can create function add objects so:
public boolean addthread(runnable r){ synchronized(_queue){ while(!dead){ _queue.addelement(r); //todo: how notify thread pool object iterate through list process queue? call notify on queue object, work on thread right?? return true } return false; } what wondering how threads executed. how can write function execute thread pool after adding thread list? also, since semaphore block after second connection, lock app until there open slot, or lock in thread pool object while looping through list?
as always, since targeting j2me/blackberry environment, pre-1.5 answers accepted, no generics or class concurrent package.
edit: take should more or less:
class mythreadpool extends thread{ private final vector _queue = new vector(); private cappedsemaphore _sem; public mywaitingthread (){ _sem = new cappedsemaphore(2); this.start(); } public void run(){ while(!dead){ runnable r = null; synchronized(_queue){ if(_queue.isempty()){ _queue.wait(); } else { r = _queue.elementat(0); _queue.removeelement(0); } } if(r != null){ _sem.take(); r.run(); _sem.release(); } } } public boolean addthread(runnable r){ synchronized(_queue){ if(!dead){ _queue.addelement(r); _queue.notifyall(); return true } return false; } }
what want do, in on thread side have each thread wait on queue. example
class mywaitingthread extends thread{ private final queue _queue; public mywaitingthread (queue _queue){ this._queue = _queue; } public void run(){ while(true){ runnable r = null; synchronized(_queue){ if(_queue.isempty()) _queue.wait(); else r = queue.pop(); } if(r != null) r.run(); } } } and in other logic like:
public void addthread(runnable r){ if(!dead){ synchronized(_queue){ _queue.addelement(r); _queue.notifyall(); } } } that _queue.notifyall wake threads waiting on _queue instance. also, notice moved while(!dead) outside of synchronized block , changed if(!dead). can imagine keeping way had wouldnt have worked hoped.
Comments
Post a Comment