Cannot refer to a non-final variable file inside an inner class defined in a different method

Source: Internet
Author: User

Java Multithreaded Learning:
Producers and consumers


Problem:
Cannot refer to a non-final variable file inside an inner class defined in a different method
If you define a local inner class, and the local inner class uses an object that is defined externally, why does the compiler require that its parameter reference be final?
Note: The local inner class, including the anonymous inner class.


Write yourself the problem of production and consumer class encounters, final Goods pru = new Goods ();
Did not add final, inside Pru.produce (str) before goods;
Always error cannot refer to a non-final variable file inside an inner class defined in a different method
Thread thread1 = new Thread (new Runnable () {
Publicvoid Run () {
while (pru.num<=20) {
Pru.produce (str);

}
}


Package test;

public class Pruconsumer {

/**
* @param the game of args producers and consumers
* @author Zhuyh
*/
public static void Main (string[] args) {
Class goods{
private int num;
private String name;
private Boolean flags =false;

public synchronized void Produce (String name) {
while (flags) {
try{

Wait ();

}catch (Interruptedexception e) {
E.printstacktrace ();

}
}
THIS.name = name+ "--number" + num++;
System.out.println (Thread.CurrentThread (). toString () + "produced:" +this.name);
flags= true;
Notifyall ();

}


Public synchronized void consumer () {
while (!flags) {
try{

Wait ();

}catch (Interruptedexception e) {
E.printstacktrace ();

}
}
System.out.println (Thread.CurrentThread (). toString () + ": Consumed:" +this.name);
Flags= false;
Notifyall ();

}

}

TODO auto-generated Method Stub
Final Goods pru = new Goods ();
Final String str= "commodity";
Thread thread1 = new Thread (new Runnable () {
Publicvoid Run () {
while (pru.num<=20) {
Pru.produce (str);

}
}



}, "producer number One");
Thread1.start ();

Thread thread2 = new Thread (new Runnable () {
Publicvoid Run () {
while (pru.num<=20) {
Pru.produce (str);

}
}
}, "producer No. Second");
Thread2.start ();

Thread thread3 = new Thread (new Runnable () {
Publicvoid Run () {
while (pru.num<=20) {
Pru.consumer ();

}
}
}, "consumer number One");
Thread3.start ();

Thread thread4 = new Thread (new Runnable () {
Publicvoid Run () {
while (pru.num<=20) {
Pru.consumer ();

}
}
}, "consumer number second");
Thread4.start ();


}

}

Find explanations Online
Each process in the JVM will have more than one root, each static variable, method parameter, local variable, and of course this refers to a reference type. The underlying type is not a root, and the root is actually a storage address. The garbage collector starts by traversing the objects it references and marking them from the root, so recursively to the nearest After all the roots have been traversed, the object description that is not tagged is not referenced, so it is the object that can be reclaimed (some objects have finalized methods, although there is no reference, However, there is a dedicated queue in the JVM that references them until the finalized method is executed before it is removed from the queue as a truly unreferenced object and can be recycled, regardless of what is discussed in this topic, including the partitioning of generations, and so on. This looks good, but in the internal class callback method, S is neither a static variable nor a temporary variable in a method, it is not a method parameter, it cannot be a root, there is no variable in the inner class to reference it, its root is in that method outside the inner class, if the outside variable s is pointing to another object, the object s in the callback method loses its reference and may be recycled , and since most of the internal class callback methods are executed in other threads, it may also continue to be accessed after recycling. What will this result be? Instead, using the final modifier not only keeps the object's references unchanged, The compiler also maintains the lifetime of this object in the callback method. So that's the fundamental meaning of the final and final parameters.

Cannot refer to a non-final variable file inside an inner class defined in a different method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.