1 4 package com.tctest.spring.bean; 5 6 public class BeanWithAutolock implements IActiveBean, Runnable { 7 private String value = "0"; 8 private Object mutex = new Object (); 9 10 private transient boolean started; 11 private transient boolean active; 12 private transient String localValue; 13 14 public String getValue() { 15 synchronized (this) { 16 return this.value; 17 } 18 } 19 20 public void setValue(String value) { 21 this.localValue = value; 22 } 23 24 public boolean isActive() { 25 return active; 26 } 27 28 public void start() { 29 this.started = true; 30 31 Thread thread = new Thread (this); 32 thread.start(); 33 } 34 35 public void stop() { 36 this.started = false; 37 } 38 39 public void run() { 40 try { 41 synchronized (mutex) { 42 while (started) { 43 this.active = true; 44 if (this.localValue != null) { 45 this.value = this.localValue; 46 this.localValue = null; 47 } 48 49 try { 50 Thread.sleep(100L); 51 } catch (InterruptedException e) { 52 } 54 } 55 } 56 } finally { 57 this.active = false; 58 } 59 } 60 61 } 62 | Popular Tags |