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


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

Re: A question about synchronized threads

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!postnews.google.com!d26g2000prn.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 09:09:48 -0700 (PDT)
Organization http://groups.google.com
Lines 81
Message-ID <f56d0826-0b46-4822-9059-22159aebeec2@d26g2000prn.googlegroups.com> (permalink)
References <3f249d87-aaf8-4732-9ee8-fd112cf82553@f31g2000pri.googlegroups.com> <ipen7h$dip$1@dont-email.me>
NNTP-Posting-Host 203.252.106.182
Mime-Version 1.0
Content-Type text/plain; charset=EUC-KR
Content-Transfer-Encoding quoted-printable
X-Trace posting.google.com 1304093388 31001 127.0.0.1 (29 Apr 2011 16:09:48 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Fri, 29 Apr 2011 16:09:48 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info d26g2000prn.googlegroups.com; posting-host=203.252.106.182; 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:3357

Show key headers only | View raw


On 4월30일, 오전12시58분, Daniele Futtorovic <da.futt.n...@laposte-dot-
net.invalid> wrote:
> On 29/04/2011 17:35, byhesed allegedly 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().
>
> > 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?
>
> Yes, it is correct.
>
> > If it is correct, how can I handle it better?
> > I think it is too ineffectual. Does anybody know?
>
> By defining yourself the monitor you synchronise on.
>
> When you declare a method synchronized, the code will use the instance
> of which that method is a member as the monitor. In other words, the
> code above is equivalent to this:
>
>   public class A {
>     void m1() {
>       synchronized( this ){ ... }
>     }
>     void m2() {
>       synchronized( this ){ ... }
>     }
>     void m3() { ... }
>   }
>
> Both method lock on the same monitor, so when one is executed, no other
> thread can execute any of them.
>
> If you don't want that interdependency, you can define monitors yourself:
>
>   public class A {
>     private final Object
>       m1Monitor = new Object(),
>       m2Monitor = new Object()
>     ;
>     void m1() {
>       synchronized( m1Monitor ){ ... }
>     }
>     void m2() {
>       synchronized( m2Monitor ){ ... }
>     }
>     void m3() { ... }
>   }
>
> That way, a thread executing m1 will not prevent another thread from
> executing m2, only m1 itself.
>
> --
> DF.
> An escaped convict once said to me:
> "Alcatraz is the place to be"

Thank you. I understand what you elaborated on.

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