1 16 17 package test.concurrency; 18 19 26 public class TestService { 27 private static Object lock = new Object (); 28 29 private static TestService singleton = null; 30 public static final String MESSAGE = "Hi there, come here often?"; 31 32 public TestService() throws Exception { 33 synchronized (lock) { 34 if (singleton != null) { 35 throw new Exception ("Multiple instances of TestService created!"); 37 } 38 39 singleton = this; 40 } 41 } 42 43 public String hello() { 44 return MESSAGE; 45 } 46 } 47 | Popular Tags |