Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39168
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Thread and synchonized question |
| Date | 2019-09-30 19:48 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <qmu48o$1a50$1@gioia.aioe.org> (permalink) |
| References | <55e14769-2ee7-4e11-8f66-69c1b7bf0241@googlegroups.com> |
On 9/30/2019 3:32 AM, mike 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?
>
> 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?
The key thing to understand is that locks are always on an object.
Two threads trying to lock the same object get impacted.
Two threads trying to lock on different objects are not impacted.
The base syntax is:
synchronized(mylockobject) {
}
But there are two short versions where the object is implicit:
public class C {
...
public synchronized void m() {
...
}
...
}
means:
public class C {
...
public void m() {
synchronized (this) {
...
}
}
...
}
and:
public class C {
...
public static synchronized void m() {
...
}
...
}
means:
public class C {
...
public static void m() {
synchronized (C.class) {
...
}
}
...
}
So look at your code and determine what objects are being locked on.
Arne
PS: Remember that the locking mechanism is only a synchronization
mechanism. Locking on an objects does not restrict how that object
can be used by the code.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll 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