I am confused about using static method in Multithreading java?

有些话、适合烂在心里 提交于 2019-11-29 13:38:52

问题


something about static:

  • instances of class share static method

the similar questions:

  • Java: when to use static methods
  • What does the 'static' keyword do in a class?

I am confusing about:

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

回答1:


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.




回答2:


static method just have only one memory block?

No, methods don't have memory blocks. Threads executing those methods do. Each thread will have it's own memory on the stack where it stores all the method arguments and variables.

if i use static method in multithreading, will it block

A thread cannot access the memory of another thread, but if there is some resource that belongs to all instances and is supposed to be accessed sequentially, then you can synchronize or lock the static method, thus making it a blocking one. Otherwise, no.




回答3:


Each thread has its own stack space, each time a thread calls a method (static or virtual) that call allocates a stack frame, which holds local variables. nothing about this is specific to static methods.

Static methods can be called concurrently by multiple threads, unless you specifically do something to thwart that, such as requiring that the caller acquire a lock (such as using the synchronized keyword).

Static methods are good for cases where there is no shared state. They may be ok in cases accessing or modifying threadsafe shared state, depending on what level of concurrency is needed and how efficient the threadsafe things being accessed are.

Look out for bottlenecks. Putting the synchronized keyword on a static method may be a problem as that limits your application to calling it with only one thread at a time. Alternative strategies including using atomic objects, using threadsafe data structures designed for high concurrency, or using thread confinement may be preferable to locking.



来源:https://stackoverflow.com/questions/41071976/i-am-confused-about-using-static-method-in-multithreading-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!