1 4 package com.tctest.spring.bean; 5 6 import java.io.Serializable ; 7 import java.util.ArrayList ; 8 import java.util.List ; 9 10 public class Singleton implements ISingleton, Serializable { 11 private transient String transientValue = "aaa"; 12 private transient boolean transientBoolean = true; 13 14 private int counter = 0; 15 private boolean sharedBoolean; 16 17 private List recorder; 18 private List localRecorder = new ArrayList (); 19 private transient List transientRecorder = new ArrayList (); 20 21 public void afterPropertiesSet() throws Exception { 23 final String msg = "afterPropertiesSet"; 24 26 record(recorder, msg); 27 record(localRecorder, msg); 28 record(transientRecorder, msg); 29 } 30 31 public void destroy() throws Exception { 33 final String msg = "destroy"; 34 36 record(recorder, msg); 37 record(localRecorder, msg); 38 record(transientRecorder, msg); 39 } 40 41 private void record(List r, String msg) { 42 43 if (r != null) { 44 synchronized (r) { 45 r.add(Thread.currentThread().getName() + " " + msg); 46 } 47 } 48 } 49 50 public void setRecorder(List recorder) { 51 this.recorder = recorder; 52 } 53 54 public List getRecorder() { 55 return recorder; 56 } 57 58 public List getLocalRecorder() { 59 return localRecorder; 60 } 61 62 public List getTransientRecorder() { 63 return transientRecorder; 64 } 65 66 public synchronized int getCounter() { 68 return counter; 69 } 70 71 public synchronized void incrementCounter() { 72 this.counter++; 73 } 74 75 public String getTransientValue() { 76 return transientValue; 77 } 78 79 public void setTransientValue(String transientValue) { 80 this.transientValue = transientValue; 81 } 82 83 84 public String toString() { 85 return "Singleton:" + counter + " " + transientValue; 86 } 87 88 public synchronized boolean toggleBoolean() { 89 return (sharedBoolean = !sharedBoolean); 90 } 91 } 92 | Popular Tags |