分布式系统课程笔记——2

附代码

package main

import (

“fmt”

“sync”

)

//

// Several solutions to the crawler exercise from the Go tutorial

// https://tour.golang.org/concurrency/10

//

//

// Serial crawler

//

func Serial(url string, fetcher Fetcher, fetched map[string]bool) {

if fetched[url] {

return

}

fetched[url] = true

urls, err := fetcher.Fetch(url)

if err != nil {

return

}

for _, u := range urls {

Serial(u, fetcher, fetched)

}

return

}

//

// Concurrent crawler with shared state and Mutex

//

type fetchState struct {

mu sync.Mutex

fetched map[string]bool

}

func ConcurrentMutex(url string, fetcher Fetcher, f *fetchState) {

f.mu.Lock()

already := f.fetched[url]

f.fetched[url] = true

f.mu.Unlock()

if already {

return

}

urls, err := fetcher.Fetch(url)

if err != nil {

return

}

var done sync.WaitGroup

for _, u := range urls {

done.Add(1)

u2 := u

go func() {

defer done.Done()

ConcurrentMutex(u2, fetcher, f)

}()

//go func(u string) {

// defer done.Done()

// ConcurrentMutex(u, fetcher, f)

//}(u)

}

done.Wait()

return

}

func makeState() *fetchState {

f := &fetchState{}

f.fetched = make(map[string]bool)

return f

}

//

// Concurrent crawler with channels

//

func worker(url string, ch chan []string, fetcher Fetcher) {

urls, err := fetcher.Fetch(url)

if err != nil {

ch <- []string{}

} else {

ch <- urls

}

}

func master(ch chan []string, fetcher Fetcher) {

n := 1

fetched := make(map[string]bool)

for urls := range ch {

for _, u := range urls {

if fetched[u] == false {

fetched[u] = true

n += 1

go worker(u, ch, fetcher)

}

}

n -= 1

if n == 0 {

break

}

}

}

func ConcurrentChannel(url string, fetcher Fetcher) {

ch := make(chan []string)

go func() {

ch <- []string{url}

}()

master(ch, fetcher)

}

//

// main

//

func main() {

fmt.Printf(“=== Serial===\n”)

Serial(“http://golang.org/”, fetcher, make(map[string]bool))

fmt.Printf(“=== ConcurrentMutex ===\n”)

ConcurrentMutex(“http://golang.org/”, fetcher, makeState())

总结

阿里伤透我心,疯狂复习刷题,终于喜提offer 哈哈~好啦,不闲扯了

image

1、JAVA面试核心知识整理(PDF):包含JVMJAVA集合JAVA多线程并发,JAVA基础,Spring原理微服务,Netty与RPC,网络,日志,ZookeeperKafkaRabbitMQ,Hbase,MongoDB,Cassandra,设计模式负载均衡数据库一致性哈希JAVA算法数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算共30个章节。

image

2、Redis学习笔记及学习思维脑图

image

3、数据面试必备20题+数据库性能优化的21个最佳实践

image
abbitMQ**,Hbase,MongoDB,Cassandra,设计模式负载均衡数据库一致性哈希JAVA算法数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算共30个章节。

[外链图片转存中…(img-v7VytorT-1721173354271)]

2、Redis学习笔记及学习思维脑图

[外链图片转存中…(img-CdUzssIa-1721173354272)]

3、数据面试必备20题+数据库性能优化的21个最佳实践

[外链图片转存中…(img-ofxiTfFv-1721173354272)]

  • 25
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值