Understanding Variable Scope
By admin on Jul 21, 2008 in Java, Programming
Once in awhile, I came across bugs like these in applications.
This piece of code has concurrency issue in a multithreaded environment with Java RMI.
public class RMIServer extends UnicastRemoteObject implements Remote{
private String stringVar;
public RMIServer () throws RemoteException {
}
public RMIServer (int port) throws RemoteException {
super(port);
}
public boolean process(Map<String, String> context) throws RemoteException {
stringVar = context.get("value");
}
The variable stringVar will have concurrency issue since the process method can be called by many threads at the same time.
This code suffered has memory leak issue
public class ReadBinaryFile {
private byte[] byteArray;
public void readFile(String fileName) {
if (byteArray == null)
byteArray = new byte[len];
}
The buffer is never released because it is always reachable by the program until the ReadBinaryFile object is garbage collected.
Post a Comment