Advertisement
Guest User

Untitled

a guest
May 6th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. public class TraceApplicationContext {
  2. private volatile static TraceApplicationContext instance = new TraceApplicationContext();
  3. private TraceApplicationContext() {
  4.  
  5. }
  6. public static TraceApplicationContext getInstance() {
  7. return instance;
  8. }
  9. }
  10.  
  11. public class SpoutA extends BaseRichSpout {
  12. public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
  13. TraceApplicationContext.getInstance().init();
  14. }
  15. }
  16.  
  17. public class BoltA extends BaseRichBolt {
  18. private static JedisCluster jedisCluster = TraceApplicationContext.getInstance().getJedisCluster();
  19. }
  20.  
  21. public class BoltB extends BaseRichBolt {
  22. private static JedisCluster jedisCluster = TraceApplicationContext.getInstance().getJedisCluster();
  23. }
  24.  
  25. public class MySingleton {
  26. private static MySingleton instance;
  27. private MySingleton() { ... }
  28. public static MySingleton getInstance() {
  29. if (instance == null) { instance = new MySingleton() }
  30. return instance;
  31. }
  32. }
  33.  
  34. public enum MySingleton {
  35. INSTANCE;
  36.  
  37. private MySingleton() {...}
  38. }
  39.  
  40. public class MySingleton {
  41. private static volatile MySingleton instance;
  42. private MySingleton() { ... }
  43. public static MySingleton getInstance() {
  44. if (instance == null) {
  45. synchronized(MySingleton.class) {
  46. if (instance == null) {
  47. instance = new MySingleton();
  48. }
  49. }
  50. }
  51. return instance;
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement