2020-08-08

单机版外部排序

基础

Hello World单线程网络版

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
		fmt.Fprintln(writer, "

Hello world!

"
) }) http.ListenAndServe(":8888", nil) }

访问: http://localhost:8888/

Hello World多线程版

  • 第一种:(没有输出)
import "fmt"

func printHelloWorld()  {
   fmt.Println("Hello World!")
}
func main() {
   go printHelloWorld()
}
  • 第2种:(输出: Hello World!)
package main

import (
	"fmt"
	"time"
)

func printHelloWorld()  {
	fmt.Println("Hello World!")
}
func main() {
	go printHelloWorld()
	time.Sleep(time.Microsecond)
}
  • 第3种:
package main

import (
	"fmt"
	"time"
)

func printHelloWorld(i int)  {
	for {
		fmt.Println("Hello World from goroutine " , i)
	}
}
func main() {
	for i := 0; i < 5; i++ {
		// 编译器级别的多任务(多个goroutine同时映射到一个物理线程)
		go printHelloWorld(i)
		// 1ms的时间5个人争抢输出: 输出顺序不确定,输出个数不确定,哪个线程抢多了也不确定
	}

	time.Sleep(time.Microsecond)
}

你可能感兴趣的:(#,golang)