Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: data shared between threads and synchronized on different objects Date: Tue, 02 Aug 2011 08:54:41 -0700 Organization: A noiseless patient Spider Lines: 23 Message-ID: References: <8ac6f882-278b-46ab-a611-70130666c324@l37g2000yqd.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 2 Aug 2011 15:54:46 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="29081"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Emqg/pufaJLIhKRWtEOeVwp6RTIgc7xo=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: <8ac6f882-278b-46ab-a611-70130666c324@l37g2000yqd.googlegroups.com> Cancel-Lock: sha1:wXCCUxu5Lf8MjwTll7a2C047GXw= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6751 On 8/2/2011 8:06 AM, Marcin Rodzik wrote: > - due to discovering that the queue (line 19) is empty the output > stream is flushed > - during flush some objects are added to the queue and flag I didn't look at your code, but this shouldn't be a problem. What kind of queue are you using? You should be using something like BlockingQueue. 1. Poll the queue, send if not empty; repeat until empty. 2. If empty, flush 3. After flushing, block on the queue. When new items are added, you'll wake up. You'll also wake up right away if some item was added to the queue in the meantime. That's it. Once you have a correctly synchronized queue, all the worries go away. If you can't use BlockingQueue for some reason, then you should code an object that works like it yourself. It shouldn't be terribly difficult.