JAVA中的静态方法是创建单个实例吗? [英] Is static method in JAVA creates a single instance?
问题描述
我有一个疑问。
假设在一个多线程环境中,10K用户同时使用一个站点,并且该站点有一个静态方法。
如果JAVA中的静态方法创建了一个单个实例,然后第10k用户需要等待该方法,直到其余用户完成其使用。
我是对的吗?
有人可以解释一下吗?
I have a doubt.
Suppose in a multithreaded environment 10K users are using a site simultaniously and the site has a static method.
If a static method in JAVA creates a single instance, then the 10k-th user needs to wait for the method until rest of the users complete their usage.
Am I right?
Can anybody please explain?
推荐答案
如果JAVA中的静态方法创建单个实例,则第10K个用户需要等待该方法其余的usres完成使用。
If a static method in JAVA create a single instance, then 10K th user need to wait for the method untill rest of the usres complete there usage.
调用静态方法不会隐式创建实例。当然,你可以在方法中 - 但你不必这样做。
Calling a static method doesn't create an instance implicitly. You can do so within the method, of course - but you don't have to.
创建实例也不需要锁定 - 尽管如此,您可以根据需要添加同步。
Nor does creating an instance require a lock - although again, you can add synchronization should you wish to.
因此,对于不需要对共享数据进行任何同步访问的简单静态方法,应该多个线程同时调用该方法没有问题。
So in the case of a simple static method which doesn't need any synchronized access to shared data, there should be no problem with multiple threads calling that method concurrently.
这篇关于JAVA中的静态方法是创建单个实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!