Java 是否隐式定义了对类中使用的对象的引用?
看了书,上网查了一下Java中的引用类型,我还是有一些疑惑(或者可能是我对这个概念的理解错误)。
如果有人消除我的疑虑,那对我来说会有很大的帮助。
让我举一个包含类变量、实例变量和局部变量的类的例子。
public class Test {
public static ArrayList<String> listCommon = new ArrayList<String>();
private HashMap<String, String> mapInstance;
public Test() {
mapInstance = new HashMap<String, String>();
}
public void doSomething(String key) {
ArrayList<String> local = new ArrayList<String>();
if(key != null){
local.add(mapInstance.get(key));
}
System.out.println("Value is added in instance Map: ", mapInstance.get(key));
}
}
我的问题是;
1. listCommon
(静态变量)和mapInstance
(实例变量)是对垃圾收集器的强引用吗?
2. 变量local
(在方法中定义和使用)是弱引用吗?
3. 幻像参考和软参考是如何出现的?
4、OR以上3个概念无效;是否意味着只有显式使用 java.lang.ref 包中定义的类型时,Java 才会定义引用?
任何帮助对我来说都是很大的。
After reading the books, surfing the nets regarding the type of references in Java, I still have some doubts (or I may have interpreted the concept wrong).
It would be a great help for me if anyone clear my doubts.
Let me take an example of a class containing class variables, instance variables and local variables.
public class Test {
public static ArrayList<String> listCommon = new ArrayList<String>();
private HashMap<String, String> mapInstance;
public Test() {
mapInstance = new HashMap<String, String>();
}
public void doSomething(String key) {
ArrayList<String> local = new ArrayList<String>();
if(key != null){
local.add(mapInstance.get(key));
}
System.out.println("Value is added in instance Map: ", mapInstance.get(key));
}
}
My Question are;
1. are listCommon
(static variable) and mapInstance
(instance variable) Strong reference towards Garbage Collector?
2. Is variable local
(defined and used in method) a Weak reference?
3. How the Phantom reference and Soft reference came in picture?
4. OR above 3 concepts are invalid; means that Java defines the references only if you explicitly used the type defined in java.lang.ref package?
Any help would be great for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,它们是强有力的参考。
不,它是一个局部变量,所以它是一个变量,所以它仍然是一个强引用。
如果你使用它们。如果您不使用它们,则无需担心它们。它们实际上是用于编写各种类型的缓存。
参考变量总是很强的。其他类型仅在您明确使用它们时才会出现。
They are strong references, yes.
No, it is a local variable, so it is a variable, so it is still a strong reference.
If you use them. If you don't use them you don't need to worry about them. They are for writing various kinds of caches really.
Reference variables are always strong. The other kinds only arise when you use them explicitly.
回答问题:
是的,它们具有很强的参考性。弱引用是使用 弱引用 定义的目的。
否,原因为 1)。
Phantom Reference 没有不适用于您的示例。默认情况下,所有实例都是强引用。
是的,您可以使用
java.lang.ref
包指定弱/幻像引用。默认情况下,使用强引用。注意:维基百科对弱引用的解释可能会有用。
Answer to questions:
Yes, they are strong reference. Weak Reference are defined using Weak reference Object.
No, reason as 1).
Phantom Reference doesn't apply in your example. By default, all instances are strong reference.
Yes, you specify weak/phantom reference using the
java.lang.ref
package. By default, strong reference is used.Note: A wikipedia explaination of Weak Reference might be useful.
阅读这篇有关变量引用的博客文章。
** 更新 **
原子引用是以原子方式访问的方式引用的对象;这意味着没有竞争条件访问它。例如:
将生成两个线程,1)将增加
count
并 2)将减少它。现在,两个线程将并行运行,程序将等待直到两个线程终止(在join
点)。一个常见的想法是,由于两个线程具有相同的循环(将循环相同的时间),因此每个线程都会“取消”彼此的操作,因此count
应该为 0,但这不是案件。为什么?因为,例如,t1
可能想要增加变量,这样的操作不是原子的,而是更多的顺序:get
,inc,<代码>设置。因此,在某些情况下,即使在进行第二个操作
实际上会有inc
之前,t2
也会访问变量并dec
变量,因此 < code>t1+2
变量。反之亦然,以无限期的方式。 (每次运行代码都会产生不同的值。)现在,将
int
替换为AtomicInteger
并使用incrementAndGet()
或decrementAndGet ()
将通过将get
、inc
、set
渲染为单个操作来解决该问题,并消除其中的竞争条件代码。Read this blog post about variable references.
** Update **
An atomic reference is an object referenced in such a way that it is accessed atomically; meaning that there are no race condition accessing it. For example:
Will spawn two threads that 1) will increment
count
and 2) will decrement it. Now, both threads will run in parallel and the program will wait until both terminate (at thejoin
point). A common idea would be that since both threads have the same loop (will loop the same amount of time), each will "cancel" each other's operation and thuscount
should be 0, but this is not the case. Why? Because while, for example,t1
may want to increase the variable, such operation is not atomic, but is more in the order of :get
,inc
,set
. So there may be cases that, even before the second operationinc
is made,t2
would have accessed the variable anddec
the variable, thust1
would have had actually+2
the variable. And vice versa in an indefinite manner. (Each run of the code would produce a different value.)Now, replacing
int
byAtomicInteger
and usingincrementAndGet()
ordecrementAndGet()
will solve the issue by rendering theget
,inc
,set
into a single operation, and eliminating the race condition in the code.