Created
October 23, 2016 00:43
-
-
Save jeffrade/1d90d51d76971749da36bfed6961d9d3 to your computer and use it in GitHub Desktop.
Example of a Java class that is NOT thread-safe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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