Created
September 6, 2016 04:52
-
-
Save jayjaykim/cee686e1160bccb7cc6a2c0254bc603d to your computer and use it in GitHub Desktop.
TestSingleton.java which is imperfect in multi-threading environment
This file contains hidden or 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 Communication { | |
static Communication instance; | |
private Communication() { | |
} | |
public static Communication getInstance() { | |
if(instance == null) | |
instance = new Communication(); | |
return instance; | |
} | |
public void send(String message) { | |
// sends message | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
// | |
Communication instance = Communication.getInstance(); | |
Communication communication = Communication.getInstance(); | |
instance.send("hi"); | |
communication.send("hello car!"); | |
Communication.getInstance().send("hello"); | |
Communication.getInstance().send("world"); | |
// handle two responses | |
// | |
MainSub1 mainSub1 = new MainSub1(Communication.getInstance()); | |
mainSub1.setCommunication(communication); | |
mainSub1.does(); | |
mainSub1.does1(); | |
} | |
} | |
public class MainSub1 { | |
Communication communication; | |
public MainSub1(Communication communication) { | |
this.communication = communication; | |
} | |
public void setCommunication(Communication communication) { | |
this.communication = communication; | |
} | |
void does() { | |
communication.send("hi"); | |
} | |
void does1() { | |
Communication.getInstance().send("hi"); | |
} | |
public static void main(String[] args) { | |
} | |
} | |
public class MainSub2 { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment