Home > Back-end >  Inquire of Java synchronized synchronization method of problem?
Inquire of Java synchronized synchronization method of problem?

Time:11-28

 
Class A {
Public synchronized void doSomething1 {
}
Public synchronized void doSomething2 {
}
Public synchronized static void doSomething3 {
}
}

If a class contains both synchronized methods are static and non-static synchronization method, a new class of objects, open the ab two threads, if when performing non-static synchronization method, a thread b threads don't have access to the object other than static synchronization method, this is certain,
If I want to ask is when performing non-static synchronization method, a thread b thread can perform static synchronous method? And if a thread synchronization method, static b thread can perform non-static synchronization method?

CodePudding user response:

Is static and non-static two locks, but perform static or non-static mutex,

CodePudding user response:

The static method must be in a static class, you can write like this

CodePudding user response:

refer to the second floor KeepSayingNo response:
static method must be in a static class, can you write it

A static method in a static class

CodePudding user response:

reference 1st floor icoolno1 response:
static and non-static are two locks, at the same time perform static or non-static mutex,

Is not a static lock belongs to the object, and belongs to class class, static lock does not affect the same object under different lock, but different objects are influenced by the static lock?

CodePudding user response:

references 4 floor there are N reply:
Quote: refer to 1st floor icoolno1 response:
static and non-static are two locks, at the same time perform static or non-static mutex,

Is not a static lock belongs to the object, and belongs to class class, static lock does not affect the same object under different lock, but different objects are influenced by the static lock?


Lock only have an effect on their scope, instance and static is beyond the scope of a function, can't say,

CodePudding user response:

The static way is to lock this object itself, the static method is locked this. GetClass () class object, this is two different locks
Also take this is a lock, do not affect b take this. GetClass () lock, the reverse is also true, take this. A getClass () lock, does not affect the b take this lock
Unless
A not only took this lock, greedy also took this. GetClass () lock, the b can only drink northwest breeze

CodePudding user response:

Invocation style can send to you, I am here to write a call discovery and not the same as you describe

CodePudding user response:

Static and non-static is not the same lock, the way you call is not mutually exclusive, yes,

CodePudding user response:

Add the synchronized static methods you can think of is synchronized (the name of the class. The class)
Add the synchronized non-static methods you can think of is synchronized (this)

Static methods locked, only one lock
Non-static methods lock, how many objects have how many lock

  • Related