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!r27g2000prr.googlegroups.com!not-for-mail From: byhesed Newsgroups: comp.lang.java.programmer Subject: Re: A question about synchronized threads Date: Fri, 29 Apr 2011 18:43:52 -0700 (PDT) Organization: http://groups.google.com Lines: 46 Message-ID: <2d4cb21d-ba59-48cf-897c-b9d7e61a984e@r27g2000prr.googlegroups.com> References: <3f249d87-aaf8-4732-9ee8-fd112cf82553@f31g2000pri.googlegroups.com> <8b7289b0-2b52-44f9-96a9-fe1d2661de11@k3g2000prl.googlegroups.com> NNTP-Posting-Host: 119.202.36.92 Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1304127832 3878 127.0.0.1 (30 Apr 2011 01:43:52 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 30 Apr 2011 01:43:52 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: r27g2000prr.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:3371 On 4=EC=9B=9430=EC=9D=BC, =EC=98=A4=EC=A0=8410=EC=8B=9C01=EB=B6=84, Eric So= sman wrote: > On 4/29/2011 12:12 PM, byhesed wrote: > > > [...] > > If too much spaces are marked as critical regions, > > then the program will not be optimized. > > It wastes too much time in waiting for obtaining a right to access > > critical regions. > > > So, in my question, better means optimization when using threads. > > =C2=A0 =C2=A0 =C2=A0A bit of advice I've found *very* useful over the yea= rs: Don't > think about protecting "critical regions of code," think instead > about protecting "access to shared data." > > =C2=A0 =C2=A0 =C2=A0Stop. =C2=A0Go back and read the paragraph again. =C2= =A0It's the crux. > > =C2=A0 =C2=A0 =C2=A0Threads T1,T2,...,Tn do not interfere by executing th= e same > code simultaneously, but by trying to access the same data. =C2=A0(More > generally, by trying to access the same "state.") =C2=A0If the shared > state is S, then T1,T2,...,Tn must not try to alter it at the same > time, nor try to read it while another Tx is altering it. =C2=A0If, on > the other hand, S decomposes into disjoint sub-states S1,S2,...,Sm > that are *completely* independent, then it's all right for Ti to > alter Sa while Tj reads Sb; you must guard against simultaneous > alteration or alteration-and-read of each single sub-state Sx. > > =C2=A0 =C2=A0 =C2=A0Think about the state; that's what you're trying to k= eep > coherent and consistent. =C2=A0Don't worry about the code; it's just > the tool that manipulates the state. =C2=A0You'll be astonished at how > much simpler things become with this view. =C2=A0Trust me. > > -- > Eric Sosman > esos...@ieee-dot-org.invalid Thank you for your advice. I'll remember what you recommend: "Think instead about protecting "access to shared data."