go实现并发安全hashtable 拉链法

在这个实现中,HashTable包含多个bucket,每个bucket都有一个互斥锁来保证并发安全。Put方法用于插入键值对,Get方法用于获取值,Delete方法用于删除键值对。通过哈希函数确定键应该存储在哪个bucket中,然后在对应的bucket中进行操作。这种实现方式可以有效地处理并发访问,确保哈希表在多线程环境下的正确性。

go 复制代码
package main

import (
    "sync"
)

type HashTable struct {
    buckets    []*bucket
    bucketSize int
}

type bucket struct {
    mu     sync.Mutex
    items  map[string]interface{}
}

func NewHashTable(bucketSize int) *HashTable {
    buckets := make([]*bucket, bucketSize)
    for i := range buckets {
        buckets[i] = &bucket{
            items: make(map[string]interface{}),
        }
    }
    return &HashTable{
        buckets:    buckets,
        bucketSize: bucketSize,
    }
}

func (ht *HashTable) hash(key string) int {
    hashValue := 0
    for _, char := range key {
        hashValue += int(char)
    }
    return hashValue % ht.bucketSize
}

func (ht *HashTable) Put(key string, value interface{}) {
    bucketIndex := ht.hash(key)
    bucket := ht.buckets[bucketIndex]
    bucket.mu.Lock()
    bucket.items[key] = value
    bucket.mu.Unlock()
}

func (ht *HashTable) Get(key string) (interface{}, bool) {
    bucketIndex := ht.hash(key)
    bucket := ht.buckets[bucketIndex]
    bucket.mu.Lock()
    value, ok := bucket.items[key]
    bucket.mu.Unlock()
    return value, ok
}

func (ht *HashTable) Delete(key string) {
    bucketIndex := ht.hash(key)
    bucket := ht.buckets[bucketIndex]
    bucket.mu.Lock()
    delete(bucket.items, key)
    bucket.mu.Unlock()
}



func main() {
    ht := NewHashTable(10)

    // 并发安全地插入数据
    var wg sync.WaitGroup
    for i := 0; i < 100; i++ {
        wg.Add(1)
        go func(i int) {
            defer wg.Done()
            ht.Put(fmt.Sprintf("key%d", i), i)
        }(i)
    }
    wg.Wait()

    // 并发安全地读取数据
    for i := 0; i < 100; i++ {
        wg.Add(1)
        go func(i int) {
            defer wg.Done()
            value, ok := ht.Get(fmt.Sprintf("key%d", i))
            if ok {
                fmt.Println(value)
            }
        }(i)
    }
    wg.Wait()

    // 并发安全地删除数据
    for i := 0; i < 100; i++ {
        wg.Add(1)
        go func(i int) {
            defer wg.Done()
            ht.Delete(fmt.Sprintf("key%d", i))
        }(i)
    }
    wg.Wait()
}
相关推荐
忘忧记9 分钟前
Fixture详解
开发语言·python
赵谨言10 分钟前
地球磁场干扰噪声减弱声波对抗测量系统研究进展:近十年中英文文献综述
大数据·开发语言·经验分享
jyan_敬言11 分钟前
【算法】高精度算法(加减乘除)
c语言·开发语言·c++·笔记·算法
echome88818 分钟前
Python 装饰器实战:用@syntax 优雅地增强函数功能
开发语言·python
卷Java38 分钟前
Python字典:键值对、get()方法、defaultdict,附通讯录实战
开发语言·数据库·python
liuyao_xianhui38 分钟前
优选算法_翻转链表_头插法_C++
开发语言·数据结构·c++·算法·leetcode·链表·动态规划
happy_baymax41 分钟前
三电平矢量表达式MATLAB实现
开发语言·matlab
xyq202442 分钟前
jEasyUI 创建 XP 风格左侧面板
开发语言
赫瑞43 分钟前
Java中的最长公共子序列——LCS
java·开发语言
于先生吖1 小时前
零基础开发国际版同城出行平台 JAVA 顺风车预约系统实战教学
java·开发语言