我对在多线程 java 中使用静态方法感到困惑?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41071976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:07:49  来源:igfitidea点击:

I am confused about using static method in Multithreading java?

javamultithreadingstaticstatic-methods

提问by 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?
  • 静态方法只有一个内存块?
  • 如果我在多线程中使用静态方法,它会阻塞吗?

采纳答案by scottb

I am confusing about:

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

我很困惑:

静态方法只有一个内存块?如果我在多线程中使用静态方法,它会阻塞吗?

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

staticJava 中的关键字仅表示“不考虑或不了解对象的任何特定实例”。

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

实例方法可以this用来访问其关联实例的字段,但静态方法没有关联实例,因此this没有意义。

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.

默认情况下,静态方法是非阻塞的。如果您愿意,您可以实现自己的同步/线程安全策略并拥有静态方法块。

回答by YoungSpice

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 lockthe static method, thus making it a blocking one. Otherwise, no.

一个线程不能访问另一个线程的内存,但是如果有一些资源属于所有实例并且应该顺序访问,那么您可以同步或锁定静态方法,从而使其成为阻塞方法。否则,没有。

回答by Nathan Hughes

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).

静态方法可以被多个线程并发调用,除非你专门做一些事情来阻止它,比如要求调用者获取锁(比如使用 synchronized 关键字)。

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.

注意瓶颈。将 synchronized 关键字放在静态方法上可能会出现问题,因为这会限制您的应用程序一次只能使用一个线程调用它。替代策略包括使用原子对象、使用专为高并发设计的线程安全数据结构或使用线程限制可能比锁定更可取。