Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #6769

Re: data shared between threads and synchronized on different objects

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news-transit.tcx.org.uk!news.stack.nl!.POSTED!ipv6.urchin.earth.li!twic
From Tom Anderson <twic@urchin.earth.li>
Newsgroups comp.lang.java.programmer
Subject Re: data shared between threads and synchronized on different objects
Date Tue, 2 Aug 2011 22:41:14 +0100
Organization Stack Usenet News Service
Lines 50
Message-ID <alpine.DEB.2.00.1108022226470.8354@urchin.earth.li> (permalink)
References <61899466-4def-4982-8aa9-b789aeead3ad@m22g2000yqh.googlegroups.com>
NNTP-Posting-Host ipv6.urchin.earth.li
Mime-Version 1.0
Content-Type TEXT/PLAIN; charset=US-ASCII; format=flowed
X-Trace mud.stack.nl 1312321274 75042 2001:ba8:0:1b4::6 (2 Aug 2011 21:41:14 GMT)
X-Complaints-To abuse@stack.nl
NNTP-Posting-Date Tue, 2 Aug 2011 21:41:14 +0000 (UTC)
User-Agent Alpine 2.00 (DEB 1167 2008-08-23)
In-Reply-To <61899466-4def-4982-8aa9-b789aeead3ad@m22g2000yqh.googlegroups.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6769

Show key headers only | View raw


On Tue, 2 Aug 2011, Marcin Rodzik wrote:

> I have a thread (the essential piece of its code can be found here:
> http://pastebin.com/KM8Yiqgs) which sends some objects ("tasks") over
> the network (in method tryToSendTask). Another thread submits objects
> to be sent by means of the first thread's method submit - namely, the
> "task" is put into queue.
>
> [...]
>
> What do you think?

I think you can make this a good bit simpler by using a BlockingQueue, 
such as a LinkedBlockingQueue. Any concurrency advantage a 
ConcurrentLinkedQueue might have is lost by the use of a synchronized 
method in your object.

With a BlockingQueue, the code looks more like:

public void submit(T t) {
     t.put(t);
}

public void run() {
     long timeout = Long.MAX_VALUE;
     while (goOn) {
         T t = taskSendingQueue.poll(timeout, TimeUnit.SECONDS);
         if (t != null) {
             sendTask(t);
             timeout = 0;
         }
         else {
             taskOutputStream.flush();
             timeout = Long.MAX_VALUE;
         }
     }
     completeCommunication();
}

I am mildly abusing poll here, by using extreme values of the timeout to 
effectively switch between blocking and non-blocking modes.

I don't know what this waitableClosing thing is, but i suspect you should 
combine it with goOn. A loop doesn't need more than one condition 
variable.

tom

-- 
a blood-spattered Canadarm flinging goat carcasses into the void

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

data shared between threads and synchronized on different objects Marcin Rodzik <marteno_rodia@o2.pl> - 2011-08-02 07:53 -0700
  Re: data shared between threads and synchronized on different objects Tom Anderson <twic@urchin.earth.li> - 2011-08-02 22:41 +0100
    Re: data shared between threads and synchronized on different objects markspace <-@.> - 2011-08-02 15:02 -0700
      Re: data shared between threads and synchronized on different objects Tom Anderson <twic@urchin.earth.li> - 2011-08-03 14:29 +0100
        Re: data shared between threads and synchronized on different objects Patricia Shanahan <pats@acm.org> - 2011-08-03 08:34 -0700
          Re: data shared between threads and synchronized on different objects Tom Anderson <twic@urchin.earth.li> - 2011-08-03 18:27 +0100
            Re: data shared between threads and synchronized on different objects supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-08-03 23:24 -0400

csiph-web