zoukankan      html  css  js  c++  java
  • sync lock

    ...

    sync lock

    package main
    
    import (
    	"fmt"
    	"sync"
    )
    
    type Item interface {
    }
    
    type ItemStack struct {
    	items []Item
    	lock  sync.RWMutex
    }
    
    func NewStack() *ItemStack {
    	s := &ItemStack{}
    	s.items = []Item{}
    	return s
    }
    
    func (s *ItemStack) Print() {
    	fmt.Println(s.items)
    }
    
    func (s *ItemStack) Push(t Item) {
    	s.lock.Lock()
    	s.items = append(s.items, t)
    	s.lock.Unlock()
    }
    func (s *ItemStack) Pop() (item Item) {
    	s.lock.Lock()
    	defer s.lock.Unlock()
    	sl := len(s.items)
    	if sl == 0 {
    		return nil
    	}
    	item = s.items[sl-1]
    	s.items = s.items[0 : sl-1]
    	return item
    }
    
    func main()  {
    	stack := NewStack()
    	stack.Print()
    	stack.Push(3)
    	stack.Print()
    	stack.Push(5)
    	stack.Print()
    	stack.Pop()
    	stack.Print()
    	stack.Pop()
    	stack.Print()
    }
    

    ...

    sort

    package main
    
    import (
    	"fmt"
    	"sort"
    )
    
    func main()  {
    	intlist := [] int{2,3,8,5,6,7,4,9}
    	sort.Sort(sort.Reverse(sort.IntSlice(intlist)))
    
    	float8list := []float64{4.2,5.2,6.2,7.2,1.2}
    	sort.Sort(sort.Reverse(sort.Float64Slice(float8list)))
    
    	stringlist := []string{"a","b","e","d","c"}
    	sort.Sort(sort.Reverse(sort.StringSlice(stringlist)))
    	fmt.Printf("%v
    %v
    %v",intlist,float8list,stringlist)
    }
    

      

  • 相关阅读:
    初识Kafka
    初学Linux (Linux_note)
    Zookeeper_ACL
    Zookeeper简单配置
    getWindow().setFlags
    设置session失效时间
    Android开发者必知的5个开源库
    oracle A用户访问B用户的表aa
    oracle 根据约束名查表名
    java ftp上载下传 遇到的问题
  • 原文地址:https://www.cnblogs.com/eiguleo/p/14959558.html
Copyright © 2011-2022 走看看