Depressed, refactor encounters dead endless.
I have an interface pool, as follows: public interface pool {worker getWorker ();} is used to get a Worker object from the pool. The interface of the Worker is: public interface worker {public void start (Runnable R); another interface Poolowner, as a Pool owner, you can clean up, close to Pool, close, etc. Public Interface Poolowner Extends pool {public void Close ); ...} Now, a asynchronous task processing module needs to use the Pool object. At the same time, I don't want this task to handle the ownership of the pool object, because multiple different modules may share a pool. However, I need to guarantee that the pool object is turned off when it is not used. It is of course not good to rely on Finalize.
So, here, I introduced the reference count: a Reference interface, represents the count Server, which should have a Pool ownership. public interface Referent {public void enref (); public void deref (); public Pool get ();} achieved a thread-safe code: final class ReferentImpl implements Referent {public synchronized void enref () { count;} public Synchronized void deref () {if (- count <= 0) pool.close ();} public pool get () {Return pool;} private factory poolowner pool; private int count = 1; Private ReferentImpl (Final Poolowner P) {this.pool = p;} Static Reference Instance (Final Poolowner P) {RETURN REFERENTIMPL (P);}}
In addition, we also need a representative to quote the excuse of the counting customer. Public interface ref extends pool {public ref enRef (); public void release ();} Here, the client's consideration is: 1. There is absolutely not allowed a REF object to deliver the REFERENT's count twice. Otherwise, a customer's mistakes may affect the work of other customers. EnRef will generate a new customer, while old customers still need to call Release once. My implementation is this: First, make a null reason.
final class NilReferent implements Referent {public void enref () {throw new NilRefException ();} public void deref () {} public Pool get () {throw new NilRefException ();} ... // Here, to make a singleton. } Then: final class refimpl IMPLEMENTS REF {public ref () {ref.enref (); RETURN INSTANCE ();} public void release () {ref.deref (); ref = nilreferent.instance (); Worker get () {return (). GetWorker ();} protected final void finalize () {ref.deref ();} private referent r) {this.ref = r;} Static Ref instance Reference r) {RETURN NEW REFIMPL (R);}} The code is tested as follows: Poolowner pool = ...; final ref r1 = refimpl.instance (ReferentImpl.instance (pool)); Final Ref R2 = R1. EnRef (); r1.get (); r1.release (); // Release R1R2.GET (); // Here, R2 can work R1.Release (); // Throw an exception, R1 has been called once Releaser2 .get (); // r2 can still work R2.Release (); //, the count is reduced to 0, and the pool will be turned off by R2.Get (); // Throw an exception, R2 has released.
Ok, here, everything is fine. But next. . .
I also have an interface processorpublic interface processor extend ();} This Processor provides an interrupt function, so it will require more complex logic, and there will be certain efficiency. With Processor, there ProcessorPoolpublic interface ProcessorPool {public Processor getProcessor ();} ok, I also realized Processor and ProcessorPool and, naturally ProcessorPoolOwner: public interface ProcessorPoolOwner extends ProcessorPool {public void close ();}.
Ok, now, if I have to achieve the above reference count logic, what should I do? Do you have any good way to use Copy & Paste? I feel like I am dead.