java - How to inform other objects(how to "close" queue) that there will be no more elements in ArrayBlockingQueue, -
i have arrayblockingqueue<string> queue;
, author
object puts elements in there:
public static final string end = "db8097f51282a0188e988d6bfa994430258e24ce"; [...] (string text : texts) { queue.put(text); thread.sleep(300); } queue.put(end);
and printer
objects takes elements qeue , prints them on console:
string text; { text = queue.take(); if (!text.equals(author.end)) system.out.println(text); } while (tekst != author.end);
i wanted inform printer
object there no more elements put qeue. cannot pass null
arrayblockingqueue
decided pass unusual string
inside. think bad solution.
can somehow close arrayblockingqueue
?
your solution seems fine. 2 points consider:
- if you're going have more 1 printer reading values off of same queue may want printer push end value queue other printers chance see it
- consider storing not plain string instead object of class define. class can have string field carry actual payload , additional boolean field such 'isend`. make distinction between regular items , end-of-queue item more explicit in code.
Comments
Post a Comment