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


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

Re: Thread and synchonized question

Path csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From Andreas Leitgeb <avl@logic.at>
Newsgroups comp.lang.java.programmer
Subject Re: Thread and synchonized question
Date Mon, 30 Sep 2019 13:48:00 -0000 (UTC)
Organization A noiseless patient Spider
Lines 42
Message-ID <slrnqp41sg.36g.avl@logic.at> (permalink)
References <55e14769-2ee7-4e11-8f66-69c1b7bf0241@googlegroups.com> <slrnqp3l52.36g.avl@logic.at> <50452ca1-e196-4241-be2e-55f88783ccdf@googlegroups.com>
Reply-To avl@logic.at
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
Injection-Date Mon, 30 Sep 2019 13:48:00 -0000 (UTC)
Injection-Info reader02.eternal-september.org; posting-host="57ba0cc4d71e31c6552d95816b111fab"; logging-data="16187"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/vCX3VxZXhWiET1nb8jnxr"
User-Agent slrn/1.0.3 (Linux)
Cancel-Lock sha1:8J1LPWA93G/I4tjfNBda8KDf/CA=
Xref csiph.com comp.lang.java.programmer:39166

Show key headers only | View raw


mike <mikaelpetterson@hotmail.com> wrote:
> Den måndag 30 september 2019 kl. 12:10:52 UTC+2 skrev Andreas Leitgeb:
>> mike <mikaelpetterson@hotmail.com> wrote:
>> > We have class A with method synchronized connect(int) that calls
>> > class B that has synchronized connect() (main thread)
>> > I guess the lock will be on class A?
>> 
>> There are some relevant details missing, so there are two options:
>> A) either or both of the methods are static, or there are two objects
>>      one of A and one of B that you call the method on: then the locks
>>      are independent, and main thread just hold both.
>> B) One of the methods is just a superclass method, and both "synchronize"s
>>      address the same object, and the main thread holds that one twice.
>> 
>> > What if class B and synchronized connect()is called by another
>> > thread ( reconnect thread) I guess lock is on class B.
>> 
>> > So does this mean that both main and reconnect threads can access
>> > the synchronized connect() on class B?
>> 
>> That can be pretty surely excluded in either of the above cases.
>
> Hi Andreas,
> Thx for replying and helping out to clarify.
> Yes I missed to add the information that there are two objects A and B that you call the method on. 

I forgot to mention another "caveat":

If the two threads end up calling B.connect() for *different instances*
of B (that means two separate "B" objects), then they can well be within
that method at the same time.

E.g.:
main thread:   new B().connect();
2nd thread:   new B().connect();

Then each thread has its own separate instance, so they can be in
B.connect() at the same time.

In the code of B.connect() you shouldn't rely on the "synchronized"
to access any data outside the respective "this" object.

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


Thread

Thread and synchonized question mike <mikaelpetterson@hotmail.com> - 2019-09-30 00:32 -0700
  Re: Thread and synchonized question Andreas Leitgeb <avl@logic.at> - 2019-09-30 10:10 +0000
    Re: Thread and synchonized question mike <mikaelpetterson@hotmail.com> - 2019-09-30 04:38 -0700
      Re: Thread and synchonized question Andreas Leitgeb <avl@logic.at> - 2019-09-30 13:48 +0000
  Re: Thread and synchonized question Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-09-30 17:07 -0400
  Re: Thread and synchonized question Arne Vajhøj <arne@vajhoej.dk> - 2019-09-30 19:48 -0400

csiph-web