GoCollections/main.go

76 lines
1.5 KiB
Go
Raw Normal View History

2021-05-20 17:32:35 +08:00
package main
import (
"fmt"
2021-05-21 09:48:33 +08:00
"github.com/tursom/GoCollections/collections"
"github.com/tursom/GoCollections/exceptions"
2021-05-22 17:10:26 +08:00
"time"
2021-05-20 17:32:35 +08:00
)
func main() {
2021-05-21 16:57:33 +08:00
_, err := exceptions.Try(func() (interface{}, exceptions.Exception) {
2021-05-21 09:41:58 +08:00
panic("test")
2021-05-21 16:57:33 +08:00
}, func(r interface{}) (interface{}, exceptions.Exception) {
2021-05-21 09:41:58 +08:00
fmt.Println("recover from panic", r)
2021-05-21 10:31:49 +08:00
return nil, exceptions.NewIndexOutOfBound(fmt.Sprint(r), true)
})
2021-05-21 16:57:33 +08:00
exceptions.Print(err)
2021-05-21 09:41:58 +08:00
2021-05-21 23:07:48 +08:00
list := collections.NewConcurrentLinkedQueue()
2021-05-22 17:10:26 +08:00
target := collections.NewArrayListByCapacity(10000)
fmt.Println("list", list)
go func() {
for i := 0; i < 1000000; i++ {
element, _ := list.Offer()
//fmt.Println(offer)
if element != nil {
target.Add(element)
}
}
fmt.Println("target:", target)
}()
2021-05-20 17:32:35 +08:00
2021-05-22 17:10:26 +08:00
go func() {
for i := 0; i < 1000; i++ {
err = list.Push(i)
//fmt.Println(err)
2021-05-20 17:32:35 +08:00
}
2021-05-22 17:10:26 +08:00
time.Sleep(time.Second * 2)
2021-05-20 17:32:35 +08:00
fmt.Println(list)
2021-05-22 17:10:26 +08:00
}()
//
//for i := 0; i < 100; i++ {
// fmt.Println(list)
//}
time.Sleep(time.Second * 10)
fmt.Println("target:", target)
//for i := 0; i < 20; i++ {
// list.Push(i)
// fmt.Println(list)
//}
//
//for i := 0; i < 20; i++ {
// list.Offer()
// fmt.Println(list)
//}
//
//for i := 0; i < 25; i++ {
// list.Push(i)
// fmt.Println(list)
//}
//
//_ = collections.LoopMutable(list, func(element interface{}, iterator collections.MutableIterator) (err exceptions.Exception) {
// if element.(int)&1 == 0 {
// err = iterator.Remove()
// }
// fmt.Println(list)
// return
//})
2021-05-20 17:32:35 +08:00
//for i := 0; i < 10; i++ {
// list.Remove(i * 2)
// fmt.Println(list)
//}
}