java - Consumer-Producer with Threads and BlockingQueues -


i wrote class 'producer' continuously parsing files specific folder. parsed result stored in queue consumer.

public class producer extends thread {      private blockingqueue<myobject> queue;     ...     public void run()     {         while (true)         {         //store email attachments directory         ...         //fill queue         queue.put(myobject);           sleep(5*60*1000);         }     } } 

my consumer class continuously checking if there available in queue. if so, it's performing work on parsed result.

public class consumer extends thread {      private blockingqueue<myobject> queue;     ...     public void run()     {         while (true)         {         myobject o = queue.poll();          // work on myobject 'o'         ...         sleep(5*60*1000);         }     } } 

when run programm, 'top' shows java process on 100%. guess it's because of infinite loops.

is way implement or there more resource saving way doing this?

instead of

myobject o = queue.poll(); 

try

myobject o = queue.take(); 

the latter block until there available in queue, whereas former return immediately, whether or not available.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -