示例#1
0
func NewObjectPool(factory PooledObjectFactory, config *ObjectPoolConfig) *ObjectPool {
	pool := ObjectPool{factory: factory, Config: config,
		idleObjects:             collections.NewDeque(math.MaxInt32),
		allObjects:              collections.NewSyncMap(),
		createCount:             concurrent.AtomicInteger(0),
		destroyedByEvictorCount: concurrent.AtomicInteger(0),
		destroyedCount:          concurrent.AtomicInteger(0)}
	pool.StartEvictor()
	return &pool
}
示例#2
0
func (suit *LinkedBlockDequeTestSuite) TestIteratorRemove() {
	count := 100
	suit.deque = NewDeque(count)

	for i := 0; i < count; i++ {
		suit.deque.AddFirst(i)
	}
	suit.Equal(count, suit.deque.Size())
	startWait := sync.WaitGroup{}
	startWait.Add(1)

	endWait := sync.WaitGroup{}
	endWait.Add(count + 1)

	counts := make(map[int]concurrent.AtomicInteger, count)
	hasErr := concurrent.AtomicInteger(0)
	for i := 0; i < count; i++ {
		go func(idx int) {
			startWait.Wait()
			iterator := suit.deque.Iterator()
			for iterator.HasNext() {
				item := iterator.Next()
				if item == nil {
					hasErr.IncrementAndGet()
				} else {
					c := counts[idx]
					c.IncrementAndGet()
				}
			}
			endWait.Done()
		}(i)
	}
	go func() {
		startWait.Wait()
		iterator := suit.deque.Iterator()
		c := 0
		for iterator.HasNext() {
			iterator.Next()
			if c%2 == 1 {
				iterator.Remove()
			}
			c = c + 1
		}
		endWait.Done()
	}()
	startWait.Done()
	endWait.Wait()
	iterator := suit.deque.Iterator()
	var list []int
	for iterator.HasNext() {
		item := iterator.Next().(int)
		list = append(list, item)
	}
	//fmt.Println("list:",list)
	//fmt.Println("counts:", counts)
	suit.Equal(count/2, suit.deque.Size())
	suit.Equal(count/2, len(list))
	suit.Equal(int32(0), hasErr.Get())
}
示例#3
0
func NewSleepingObjectFactory() *SleepingObjectFactory {
	return &SleepingObjectFactory{counter: concurrent.AtomicInteger(0)}
}