2016-10-02 21 views
0

如何運行10個線程,每個線程持續30秒,然後返回到程序執行?例如,我想要運行線程一段時間 - 在被殺之前

  1. 10個線程被產生並運行30秒。
  2. 然後,所有線程殺死
  3. 然後second()運行(即在所有線程執行完)

到目前爲止,我已在不過下面,我做到這一點,線程時(顯然)繼續執行和30s後CPU佔用率保持在100%:

func main(){ 
    for i := 0; i < 10; i++{ 
     go thread() 
    } 
    time.Sleep(30 * time.Second) 
    second() 
} 

func thread() { 
    for { 
     // Do stuff 
    } 
} 
+1

試試這個http://stackoverflow.com/a/6807784/1371064 –

回答

1

您可以使用Golang上下文。這是我學習時的一些代碼。

package main 

import (
    "fmt" 
    "log" 
    "time" 

    "golang.org/x/net/context" 
) 

func main() { 
    someHandler() 
} 

func someHandler() { 
    //Create a new context with a timeout duration of six seconds. You also get a cancel function you can call early. 
    //You can also have context end at a certain time, instead of a timeout 
    ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(6)) 

    for i := 0; i < 5; i++ { 
     go doStuff(ctx, i) 
    } 

    select { 
    case <- ctx.Done(): 
     log.Println("Got a cancel request") 
     return 
    case <-time.After(time.Second * time.Duration(5)): 
     //Here I'm actually ending it earlier than the timeout with cancel(). 
     log.Println("Timed out") 
     cancel() 
    } 

} 

func doStuff(ctx context.Context, num int) { 
    for { 
     select { 
     case <-ctx.Done(): 
      fmt.Println("Done working") 
      return 
     default: 
      fmt.Println("go", num, "working") 
     } 
     time.Sleep(time.Second) 
    } 
} 

輸出:

$ go run app.go 
go 0 working 
go 4 working 
go 2 working 
go 3 working 
go 1 working 
go 1 working 
go 0 working 
go 4 working 
go 3 working 
go 2 working 
go 0 working 
go 4 working 
go 2 working 
go 1 working 
go 3 working 
go 4 working 
go 3 working 
go 0 working 
go 2 working 
go 1 working 
go 3 working 
go 4 working 
go 1 working 
go 0 working 
go 2 working 
2016/10/01 23:25:23 Timed out 
1

使用等待組和「完成」的通道,取消去運行程序和並呼籲second之前同步他們的退出。關於這個主題的一些非常酷的東西可以在這裏閱讀https://blog.golang.org/pipelines

package main 

import (
    "fmt" 
    "sync" 
    "time" 
) 

func main() { 
    //create a "done" channel that will be used to signal to all go routines running thread to quit 
    done := make(chan interface{}) 

    //create a wait group to sync all go routines before continuing program execution 
    wg := new(sync.WaitGroup) 

    for i := 0; i < 10; i++ { 
     //increment waitgroup 
     wg.Add(1) 
     go thread(done, wg) 
    } 
    //zzzz... 
    time.Sleep(30 * time.Second) 
    //close the done channel 
    close(done) 
    //wait for all go routines to quit 
    wg.Wait() 
    //move on 
    second() 
} 

func thread(done chan interface{}, wg *sync.WaitGroup) { 
    defer wg.Done() 
    working := true 
    for working { 
     //use a select statement to do work until done channel is closed 
     select { 
     case <-done: 
      //end the loop 
      working = false 
     default: 
      fmt.Println("doing work...") 
     } 
    } 
} 

func second() { 
    fmt.Println("program complete") 
} 
相關問題