goroutine

How to recover from concurrent map writes?

你。 提交于 2019-11-26 21:55:10
问题 How do you recover from a runtime panic on a "concurrent map read and map write"? The usual defer with recover doesn't seem to work. Why is that? I know that you are not supposed to use maps in concurrent contexts, but still: how to recover here? Example: package main import "time" var m = make(map[string]string) func main() { go func() { for { m["x"] = "foo" } }() go func() { for { m["x"] = "foo" } }() time.Sleep(1 * time.Second) } Please add recovery code. :) 回答1: Recovering doesn't work

How does select work when multiple channels are involved?

女生的网名这么多〃 提交于 2019-11-26 18:34:53
问题 I found when using select on multiple non buffered channels like select { case <- chana: case <- chanb: } Even when both channels have data, but when processing this select, the call that falls in case chana and case chanb is not balanced. package main import ( "fmt" _ "net/http/pprof" "sync" "time" ) func main() { chana := make(chan int) chanb := make(chan int) go func() { for i := 0; i < 1000; i++ { chana <- 100 * i } }() go func() { for i := 0; i < 1000; i++ { chanb <- i } }() time.Sleep

cancel a blocking operation in Go

时光怂恿深爱的人放手 提交于 2019-11-26 18:00:53
I have a blocking operation in a 3rd party library that I don't control. It could potentially go forever. So I want to set a timeout on it. The obvious way is to wrap it with a channel and a goroutine and then select on the result with time.After . However, the problem is the goroutine running the blocking operation could potentially go forever. Here is an example to illustrate this http://repl.it/90o Is there a way to cancel a goroutine or have it garbage collected? You can't stop a goroutine from the "outside". The goroutine has to support some kind of termination signalling (most often a

Number of threads used by Go runtime

孤街浪徒 提交于 2019-11-26 16:54:35
问题 How many threads can the Go runtime (scheduler, garbage collector, etc.) use? For example, if GOMAXPROCS is 10 , how many of those kernel threads would be used by the runtime? Edit: I was reading the rationale for changing GOMAXPROCS to runtime.NumCPU() in Go 1.5. There was a sentence that claimed that “the performance of single-goroutine programs can improve by raising GOMAXPROCS due to parallelism of the runtime, especially the garbage collector.” My real question is: If I have a single

How to asynchronously call a method in Java

ⅰ亾dé卋堺 提交于 2019-11-26 15:46:39
I've been looking at Go's goroutines lately and thought it would be nice to have something similar in Java. As far as I've searched the common way to parallelize a method call is to do something like: final String x = "somethingelse"; new Thread(new Runnable() { public void run() { x.matches("something"); } }).start(); Thats not very elegant. Is there a better way of doing this? I needed such a solution in a project so I decided to implement my own wrapper class around a async method call. I published my wrapper class in J-Go . But I don't know if it is a good solution. The usage is simple:

Is it safe to read a function pointer concurrently without a lock?

天涯浪子 提交于 2019-11-26 15:30:20
Suppose I have this: go func() { for range time.Tick(1 * time.Millisecond) { a, b = b, a } }() And elsewhere: i := a // <-- Is this safe? For this question, it's unimportant what the value of i is with respect to the original a or b . The only question is whether reading a is safe. That is, is it possible for a to be nil , partially assigned, invalid, undefined, ... anything other than a valid value? I've tried to make it fail but so far it always succeeds (on my Mac). I haven't been able to find anything specific beyond this quote in the The Go Memory Model doc: Reads and writes of values

anonymous struct and empty struct

给你一囗甜甜゛ 提交于 2019-11-26 15:26:39
问题 http://play.golang.org/p/vhaKi5uVmm package main import "fmt" var battle = make(chan string) func warrior(name string, done chan struct{}) { select { case opponent := <-battle: fmt.Printf("%s beat %s\n", name, opponent) case battle <- name: // I lost :-( } done <- struct{}{} } func main() { done := make(chan struct{}) langs := []string{"Go", "C", "C++", "Java", "Perl", "Python"} for _, l := range langs { go warrior(l, done) } for _ = range langs { <-done } } [1st Question] done <- struct{}{}

Prevent the main() function from terminating before goroutines finish in Golang

↘锁芯ラ 提交于 2019-11-26 14:46:02
问题 Have loook at this contrived example: package main import "fmt" func printElo() { fmt.Printf("Elo\n") } func printHello() { fmt.Printf("Hello\n") } func main() { fmt.Printf("This will print.") i := 0 for i < 10 { go printElo() go printHello() i++ } } The output of this program would be just "This will print". Output of goroutines printElo() and printHello will not be emitted because, I guess, the main() function thread will finish before the goroutines have a chance to even start executing.

How to broadcast message using channel

不问归期 提交于 2019-11-26 12:25:55
问题 I am new to go and I am trying to create a simple chat server where clients can broadcast messages to all connected clients. In my server, I have a goroutine (infinite for loop) that accepts connection and all the connections are received by a channel. go func() { for { conn, _ := listener.Accept() ch <- conn } }() Then, I start a handler (goroutine) for every connected client. Inside the handler, I try to broadcast to all connections by iterating through the channel. for c := range ch { conn

How to asynchronously call a method in Java

为君一笑 提交于 2019-11-26 12:15:29
问题 I\'ve been looking at Go\'s goroutines lately and thought it would be nice to have something similar in Java. As far as I\'ve searched the common way to parallelize a method call is to do something like: final String x = \"somethingelse\"; new Thread(new Runnable() { public void run() { x.matches(\"something\"); } }).start(); Thats not very elegant. Is there a better way of doing this? I needed such a solution in a project so I decided to implement my own wrapper class around a async method