Skip to content

Instantly share code, notes, and snippets.

@jeffrade
Created October 23, 2016 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffrade/1d90d51d76971749da36bfed6961d9d3 to your computer and use it in GitHub Desktop.
Save jeffrade/1d90d51d76971749da36bfed6961d9d3 to your computer and use it in GitHub Desktop.
Example of a Java class that is NOT thread-safe
public class NotThreadSafe {
private static final NotThreadSafe INSTANCE = new NotThreadSafe();
private boolean isEven = false;
private NotThreadSafe() {
super();
}
public static NotThreadSafe getInstance() {
return INSTANCE;
}
public String getMessage(final int num) {
isEven = num % 2 == 0;
System.out.println("isEven=" + isEven);
if(isEven) {
System.out.println("Returning foo");
return "foo";
} else {
System.out.println("Returning bar");
return "bar";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment