当前位置: 首页 > news >正文

深圳做企业网站的百度地图排名可以优化吗

深圳做企业网站的,百度地图排名可以优化吗,专做皮鞋销售网站,建设假网站go 实现可重入锁 实际上#xff0c;Go 语言标准库中的 sync.Mutex 是不可重入的。但是#xff0c;我们可以基于 sync.Mutex 实现一个可重入锁#xff08;ReentrantLock#xff09;。下面是一个简单的可重入锁的实现示例#xff1a; Go 1package main 2 3import ( 4 Go 语言标准库中的 sync.Mutex 是不可重入的。但是我们可以基于 sync.Mutex 实现一个可重入锁ReentrantLock。下面是一个简单的可重入锁的实现示例 Go 1package main 2 3import ( 4 sync 5) 6 7type ReentrantMutex struct { 8 mutex sync.Mutex 9 holder string 10 recursion int 11} 12 13func (r *ReentrantMutex) Lock() { 14 goroutineID : getGoroutineID() // 假设这是获取当前 goroutine ID 的函数 15 16 r.mutex.Lock() 17 if r.holder goroutineID { 18 r.recursion 19 } else { 20 r.holder goroutineID 21 r.recursion 1 22 } 23} 24 25func (r *ReentrantMutex) Unlock() { 26 goroutineID : getGoroutineID() 27 28 r.mutex.Lock() 29 defer r.mutex.Unlock() 30 31 if r.holder ! goroutineID { 32 panic(attempted unlock from a goroutine that does not hold the lock) 33 } 34 35 r.recursion-- 36 if r.recursion 0 { 37 r.holder 38 } 39} 40 41// 示例性的 getGoroutineID 函数实际 Go 语言中没有直接提供获取 Goroutine ID 的方法 42func getGoroutineID() string { 43 // 这里仅作为示例真实环境中需要自己实现或使用第三方库来获取 goroutine ID 44 return goroutineID // 真实情况会更复杂 45} 46 47func main() { 48 reentrantLock : ReentrantMutex{} 49 50 go func() { 51 reentrantLock.Lock() 52 fmt.Println(Goroutine A acquired the lock.) 53 reentrantLock.Lock() 54 fmt.Println(Goroutine A reacquired the lock.) 55 reentrantLock.Unlock() 56 fmt.Println(Goroutine A released one of its locks.) 57 reentrantLock.Unlock() 58 fmt.Println(Goroutine A fully released the lock.) 59 }() 60 61 // 等待上面的 goroutine 执行完成... 62} 63 上述代码定义了一个名为 ReentrantMutex 的结构体它在内部维护了一个原始的 Mutex 和一些状态变量来跟踪持有锁的 goroutine 以及该 goroutine 对锁的递归获取次数。当同一个 goroutine 请求已经持有的锁时递归计数器增加当释放锁时如果递归计数器大于 1则递减而不完全释放锁。 请注意这里的 getGoroutineID() 函数是虚构的因为在 Go 语言中并没有直接暴露 Goroutine 的唯一标识符。在实际实现中可能需要通过其他方式来追踪锁的拥有者例如通过自定义上下文或者关联特定的数据结构等。 go lru缓存 在Go语言中要实现一个LRULeast Recently Used缓存可以使用标准库或第三方库。这里展示如何使用 container/list 和 map 结合来手动实现一个简单的LRU缓存 Go 1package main 2 3import ( 4 container/list 5 fmt 6) 7 8// LRUCache 结构体定义了一个容量限制的LRU缓存 9type LRUCache struct { 10 capacity int 11 cache map[int]*list.Element 12 list *list.List 13} 14 15// CacheItem 用于存储键值对和其在链表中的位置 16type CacheItem struct { 17 key int 18 value interface{} 19} 20 21// NewLRUCache 创建一个新的LRU缓存实例 22func NewLRUCache(capacity int) *LRUCache { 23 return LRUCache{ 24 capacity: capacity, 25 cache: make(map[int]*list.Element), 26 list: list.New(), 27 } 28} 29 30// Get 方法从缓存中获取键对应的值如果存在则更新其为最近使用 31func (c *LRUCache) Get(key int) (interface{}, bool) { 32 if elem, ok : c.cache[key]; ok { 33 c.list.MoveToFront(elem) 34 return elem.Value.(*CacheItem).value, true 35 } 36 return nil, false 37} 38 39// Put 方法将键值对添加到缓存并根据容量调整缓存内容 40func (c *LRUCache) Put(key int, value interface{}) { 41 if elem, ok : c.cache[key]; ok { 42 elem.Value.(*CacheItem).value value 43 c.list.MoveToFront(elem) 44 } else { 45 if c.list.Len() c.capacity { 46 delete(c.cache, c.list.Back().Value.(*CacheItem).key) 47 c.list.Remove(c.list.Back()) 48 } 49 newElem : c.list.PushFront(CacheItem{key: key, value: value}) 50 c.cache[key] newElem 51 } 52} 53 54// 示例代码 55func main() { 56 cache : NewLRUCache(3) 57 58 cache.Put(1, one) 59 cache.Put(2, two) 60 cache.Put(3, three) 61 62 fmt.Println(cache.Get(1)) // 输出(one, true) 63 cache.Put(4, four) // 此时键为1的项因超过容量而被移除 64 fmt.Println(cache.Get(1)) // 输出(nil, false) 65 fmt.Println(cache.Get(2)) // 输出(two, true) 66 fmt.Println(cache.Get(3)) // 输出(three, true) 67 fmt.Println(cache.Get(4)) // 输出(four, true) 68} 这个实现仅作为基础示例实际应用中可能需要考虑更多边界条件以及线程安全问题。 另外Go社区有一些成熟的第三方包如 github.com/hashicorp/golang-lru它提供了更高效且易于使用的LRU缓存实现。
http://www.pierceye.com/news/740461/

相关文章:

  • 网站建设公司模版wordpress自适应站点
  • 怎么在百度上创建网站wordpress时间轴页面
  • 网站建设公司济宁深圳互联网营销外包
  • 交互设计产品榆林网站seo
  • 唯品会网站开发招聘英文网站公司
  • 网站的推广一般有什么方式韩城网站建设韩城网站推广
  • 书城网站开发四川省建设厅网站投诉
  • 想要个网站沈阳网站备案
  • 网站建设分哪些类别谁有做爰网站号
  • 建设电子票务系统的网站需要多少钱网站开发一对一
  • 网站规划可以分成哪几步上海营销型网站制作
  • gta5 网站正在建设中新品发布会ppt
  • 做的网站每年需要续费idc网站源码
  • 备案主体负责人和网站负责人新网站 seo
  • 网站后台有什么用wordpress 不显示账号名
  • 另类小说 Wordpress长沙seo步骤
  • 网站建设7个基37网游官网
  • 网站设计存在的问题建筑设计私活平台
  • 网站如何做淘宝支付宝wordpress多站点不显示
  • 关于设计的网站免费注册公司
  • 网站建设排名北京网站排名降级的原因有哪些
  • 介绍网页设计做seo推广网站
  • 建立个人博客网站wordpress东城东莞网站建设
  • 从哪些方面建设网站泰州东方医院
  • 分类信息网站系统cmsWordPress新闻面包屑主题
  • wordpress 多标签关键字优化策略
  • idea15网站开发网站如何提升seo排名
  • 谁有网站推荐一下好安阳刚刚发生的事
  • 博客网站快速排名临邑县住房和城乡建设局网站
  • 二手网站建设方案营销网站建设服务平台