Let’s say I have a class Base with a single member message (String). Another class BaseHandler which extends this Base class. In this handler I have a method print which is setting a value to the base and printing it. At the end of call to print, I am setting the message to null.
When I create 50000 threads and run handler’s print method, I get null pointer exception once in a while.
Questions:
Why is null pointer exception thrown when I am explicitly assigning the value?
How would each threads instantiate the Base in this case?
Would the solution be marking Base.message as volatile and removing the null assignment? (in other words how to achieve thread-safety on Base.message
Please take a look at code below:
public class Base {
public String message;
}
public class BaseHandler extends Base{
protected static final Object lock = new Object();
public void printMessage( ){
synchronized ( lock ) { //This block is thread safe
System.out.println( message.toUpperCase( ) );
message = null;
}
}
}
public class Test {
public static void main(String[] args){
final BaseHandler handler = new BaseHandler();
for (int i = 0; i < 50000; i++) {
Runnable task = new Runnable(){
@Override
public void run( ) {
handler.message = "Hello world! ";
handler.printMessage( );
}
};
Thread worker = new Thread(task);
worker.setName(String.valueOf(i));
worker.start();
}
}
}
Imagine the following execution:
handler.message = "Hello world! ";handler.message = "Hello world! ";message.toUpperCase()throws a NPE.Your problem is that the 2 lines below are not atomic:
Solutions
There are several alternatives, depending on what you are trying to achieve:
synchronized(lock)block to make the 2 calls atomicprintMessage(message), removing the shared variable issue