Exemple #1
0
        public T Pick <T>(string objectType)
        {
            ObjectJar jar           = _GetObjectJar(objectType);
            var       recycleObject = jar.Pick();

            if (null == recycleObject && null != this.factory)
            {
                recycleObject = this.factory.CreateObject(objectType);

                // Load more 1/2 jar MaxCount objects
                if (!this.createOperations.Contains(objectType))
                {
                    lock (this.createOperations) {
                        this.createOperations.Add(objectType);
                    }
                    PreloadAsync(objectType, jar.MaxCount >> 1, () =>
                    {
                        lock (this.createOperations) {
                            this.createOperations.Remove(objectType);
                        }
                    });
                }
            }

            if (null != recycleObject)
            {
                this.processer.RecoverObject(recycleObject);
            }

            return((T)recycleObject);
        }
Exemple #2
0
 public CreateObjectAsyncTask(ObjectJar jar, string type, IRecycleProcesser processer, IRecycleFactory factory) : base(false)
 {
     factory.CreateObjectAsync(type, obj =>
     {
         processer.ReclaimObject(obj);
         jar.Drop(obj);
         this.completed = true;
     });
 }
Exemple #3
0
 public void Drop(string objectType, object recycleObject)
 {
     if (null != recycleObject)
     {
         this.processer.ReclaimObject(recycleObject);
         ObjectJar jar = _GetObjectJar(objectType);
         jar.Drop(recycleObject);
     }
 }
Exemple #4
0
        private ObjectJar _GetObjectJar(string type)
        {
            ObjectJar jar = null;

            if (!this.pool.TryGetValue(type, out jar))
            {
                this.pool.Add(type, jar = new ObjectJar());
            }

            return(jar);
        }
Exemple #5
0
 private void _DisposeObjectJar(ObjectJar jar)
 {
     while (jar.Count > 0)
     {
         object recycleObject = jar.Pick();
         if (null != recycleObject)
         {
             this.factory.DestroyObject(recycleObject);
         }
     }
     jar.Dispose();
 }
Exemple #6
0
 public void Preload(string objectType, int count)
 {
     if (null != this.factory)
     {
         ObjectJar jar = _GetObjectJar(objectType);
         for (var i = 0; i < count; ++i)
         {
             var recycleObject = this.factory.CreateObject(objectType);
             this.processer.ReclaimObject(recycleObject);
             jar.Drop(recycleObject);
         }
     }
 }
Exemple #7
0
        private object _Pick(string type)
        {
            ObjectJar jar           = _GetObjectJar(type);
            var       recycleObject = jar.Pick();

            if (null == recycleObject)
            {
                recycleObject = this.factory.CreateObject(type);
            }

            if (null != recycleObject)
            {
                this.processer.RecoverObject(recycleObject);
            }

            return(recycleObject);
        }
Exemple #8
0
        public void PreloadAsync(string objectType, int count, Action callback)
        {
            if (null != this.factory)
            {
                ObjectJar jar  = _GetObjectJar(objectType);
                AsyncTask task = new CompletedTask();
                for (var i = 0; i < count; ++i)
                {
                    task = task.Continue(t =>
                    {
                        return(new CreateObjectAsyncTask(jar, objectType, this.processer, this.factory));
                    });
                }

                task.Start().Continue(t =>
                {
                    if (null != callback)
                    {
                        callback();
                    }
                    return(null);
                });
            }
        }