tests/runonce/cleaner/singleton.go

35 lines
569 B
Go

package cleaner
import (
"fmt"
"sync"
"time"
)
type Singleton struct {
work bool
mx sync.Mutex
}
var instance *Singleton
var once sync.Once
func GetInstance() *Singleton {
once.Do(func() {
instance = &Singleton{}
})
return instance
}
func (s *Singleton) Clean(id int) error {
if s.mx.TryLock() {
fmt.Printf("GOT A JOB FROM ROUTINE %d !!!!!!!!!!!!!!!!!!!\n", id)
time.Sleep(time.Second)
fmt.Printf("FINISH JOB FROM ROUTINE %d !!!!!!!!!!!!!!!!!!!\n", id)
s.mx.Unlock()
} else {
fmt.Printf("NO NEW JOB FROM ROUTINE %d\n", id)
}
return nil
}