I am confused about using static method in Multithreading java?

freeeze king :

something about static:

  • instances of class share static method

the similar questions:

I am confusing about:

  • static method just have only one memory block?
  • if i use static method in multithreading, will it block?
scottb :

I am confusing about:

static method just have only one memory block? if i use static method in multithreading, will it block?

The static keyword in Java simply means "without regard or knowledge of any particular instance of an object."

An instance method can use this to access the fields of its associated instance, but a static method has no associated instance and so this makes no sense.

In multithreading, thread safety involves protecting the consistency and integrity of mutable data. Because objects encapsulate the state of their instance fields, instance methods only need to be concerned about thread safety in those circumstances in which more than one thread will be accessing the same object.

So while thread confinement of an object is a valid thread safety policy for instances of a class, this same reasoning is invalid for static methods because they have no instance.

This has nothing to do with memory blocks at all. It just has to do with access. An object instance is accessed through a reference. If the reference is thread confined, then the object to which that reference points will always be thread safe. But any thread anywhere that can access your class can potentially get to its static members because no reference to an instance is needed to use them.

Static methods are non-blocking by default. You can implement your own synchronization/thread safety policy and have your static method block if you wish.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=450413&siteId=1