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


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

Re: A question about synchronized threads

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!74.125.46.80.MISMATCH!postnews.google.com!k15g2000pri.googlegroups.com!not-for-mail
From byhesed <byhesed@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: A question about synchronized threads
Date Fri, 29 Apr 2011 18:49:26 -0700 (PDT)
Organization http://groups.google.com
Lines 50
Message-ID <0f043640-9fb3-430f-bcf6-d830a2dd6e81@k15g2000pri.googlegroups.com> (permalink)
References <3f249d87-aaf8-4732-9ee8-fd112cf82553@f31g2000pri.googlegroups.com> <68sr88-fuj.ln1@dagon.net>
NNTP-Posting-Host 119.202.36.92
Mime-Version 1.0
Content-Type text/plain; charset=EUC-KR
Content-Transfer-Encoding quoted-printable
X-Trace posting.google.com 1304128166 7617 127.0.0.1 (30 Apr 2011 01:49:26 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Sat, 30 Apr 2011 01:49:26 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info k15g2000pri.googlegroups.com; posting-host=119.202.36.92; posting-account=v_GC8QoAAABz34PprEBWdejdnnHZvg4_
User-Agent G2/1.0
X-HTTP-UserAgent Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.60 Safari/534.24,gzip(gfe)
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3372

Show key headers only | View raw


On 4월30일, 오전7시55분, da...@dagon.net (Dagon) wrote:
> byhesed  <byhe...@gmail.com> wrote:
> >public class A {
> >    synchronized void m1() { ... }
> >    synchronized void m2() { ... }
> >    void m3() { ... }
> >}
> >The book explains above code:
> >    Given an instance a of class A, when one thread is executing
> >a.m1(), another thread will be prohibited from executing a.m1() or a.m2().
>
> Throw this book away and get a better one (Goetz, "Java Concurrency in
> Practict" is good).
>
> >I have a question.
> >The explanation means than when one thread is executing m1() method,
> >No other threads can execute m1() or m2() thread.
> >Is it correct?
>
> This is kind of correct for this example, but it's stated in such a way that
> it will confuse you until you learn more completely what synchronized does.
>
> What it really does is: "before any thread enters m1 or m2, it will acquire an
> exclusive lock on the instance of A.  No other thread may acquire that lock
> for that instance of A until the lock-holding thread releases it by exiting
> the method."
>
> >If it is correct, how can I handle it better?
> >I think it is too ineffectual. Does anybody know?
>
> Depends on what you want to happen - why do you think it's ineffectual?  It's
> pretty effective if you want to make sure that only one thread at a time
> can run those methods on the same instance.
> --
> Mark Rafn    da...@dagon.net    <http://www.dagon.net/>  

I thought that synchronizing entire methods would be wasteful.
Also, although two methods need to be synchronized,
if two methods are totally unrelated to each other,
then it would be too bad, isn't it?
The rate of using CPU resources will be too low.
That is why I though it is ineffectual.

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


Thread

A question about synchronized threads byhesed <byhesed@gmail.com> - 2011-04-29 08:35 -0700
  Re: A question about synchronized threads Lew <noone@lewscanon.com> - 2011-04-29 11:53 -0400
    Re: A question about synchronized threads byhesed <byhesed@gmail.com> - 2011-04-29 09:12 -0700
      Re: A question about synchronized threads Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-29 21:01 -0400
        Re: A question about synchronized threads byhesed <byhesed@gmail.com> - 2011-04-29 18:43 -0700
  Re: A question about synchronized threads Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-04-29 17:58 +0200
    Re: A question about synchronized threads byhesed <byhesed@gmail.com> - 2011-04-29 09:09 -0700
      Re: A question about synchronized threads markspace <-@.> - 2011-04-29 11:11 -0700
        Re: A question about synchronized threads Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-04-29 20:19 +0200
          Re: A question about synchronized threads markspace <-@.> - 2011-04-29 15:21 -0700
          Re: A question about synchronized threads Patricia Shanahan <pats@acm.org> - 2011-04-29 21:10 -0700
  Re: A question about synchronized threads dagon@dagon.net (Dagon) - 2011-04-29 15:55 -0700
    Re: A question about synchronized threads Alice <quaxx1108@example.com> - 2011-04-29 20:08 -0400
    Re: A question about synchronized threads markspace <-@.> - 2011-04-29 19:07 -0700
    Re: A question about synchronized threads byhesed <byhesed@gmail.com> - 2011-04-29 18:49 -0700
      Re: A question about synchronized threads Lew <noone@lewscanon.com> - 2011-05-03 18:57 -0400
        Re: A question about synchronized threads Deeyana <d.awlberg@hotmail.invalid> - 2011-05-04 00:17 +0000
          Re: A question about synchronized threads Lew <noone@lewscanon.com> - 2011-05-04 12:45 -0400
            Re: A question about synchronized threads Deeyana <d.awlberg@hotmail.invalid> - 2011-05-04 19:59 +0000

csiph-web