I am creating some Java codes like the following:
new Thread() {
@Override public void run() {
for(;;) {
DataPkg data_pkg = new DataPkg(datapkg_passport, new Data);
try{
int len = channel.read(data_pkg); //block read from the channel
if (len > 0) writeWs(data_pkg);
if (len == -1) {
channel.close();
writeWs(new SigPkg(sigpkg_passport, "201")); //channel termination notification
break;
}
} catch(Exception e) {e.printStackTrace();}
}
Thread.interrupted();
}
}.start();
...but the compiler has given me an error: "local variables referenced from an inner class must be final or effectively final". I know similar questions been presented again and again on the web and even here, but I just did not find a good answer.
I think this is because the VM needs to make sure that the thread should not use any variable dynamically created by a method who may terminated lately and the reference to the memory location becomes invalid and causes problem.
But in my case I really need a dedicated thread on the newly created channel to keep feeding data from the peer side of the channel and feed back to the server side, and that channel is very preferred to be dynamically created since it is not a simple socket.
Is there any solution or work-around for this situation?