go语言-goroutine创建 主协程/子协程 runtime.Gosched runtime.Goexit runtime.GOMAXPROC(二十)

 

1. 创建goruoutine


func newTask() {
	for {
		fmt.Println("this is new task")
		time.Sleep(time.Second) //延时1s
	}
}

func main() {

	go newTask() //❤️建一个协程,新建一个任务

	for {
		fmt.Println("this is a main goroutine")
		time.Sleep(time.Second) //延时1s
	}

}

输出

this is a main goroutine
this is new task
this is a main goroutine
this is new task
this is new task
this is a main goroutine
this is new task
this is a main goroutine
this is new task
this is a main goroutine
this is new task
this is a main goroutine
this is new task
this is a main goroutine
this is new task
this is a main goroutine
this is new task
this is a main goroutine

 2. 主协程先退出,其它子协程夜会跟着退出

//主协程退出了,其它子协程也要跟着退出
func main() {

	go func() {
		fmt.Println("this is a new task")
		time.Sleep(time.Second)
	}() //() 代表调用

	i :=0
	for {
		i++
		fmt.Println("main this is a main task i=", i)
		time.Sleep(time.Second)
		if i == 3 {
			break
		}
	}

}

3. runtime.Gosched

让出时间片,先让别的协程执行,它执行完,在回来执行此协程

func main() {

	go func() {
		for {
			fmt.Println("this is new task")
		}
	}()

	for i:=0; i<5; i++ {
		//让出时间片,先让别的协程执行,它执行完,在回来执行此协程
		runtime.Gosched()
		fmt.Println("this is main task i=", i)
	}

}

4. runtime.Goexit

终止所在的协程

func main() {

	go func() {
		for i:=0; i<5; i++ {
			fmt.Println("this is a new task, i=", i)
			time.Sleep(time.Second)

			if i == 3 {
				runtime.Goexit() //终止所在的协程
			}
		}
	}()

	//死循环,目的不让主协程退出
	for {
	}

}

5. runtime.GOMAXPROCS

指定以x核运算
func main() {

	runtime.GOMAXPROCS(4) //指定以x核运算

	go func() {
		for {
			fmt.Print(1)
		}
	}()

	for {
		fmt.Print(0)
	}

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ERROR Failed to compile with 48 errors 上午10:53:54 These dependencies were not found: * core-js/modules/es.array.push.js in ./node_modules/.store/@babel+runtime@7.22.6/node_modules/@babel/runtime/helpers/esm/objectSpread2.js, ./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/.store/babel-loader@8.3.0/node_modules/babel-loader/lib!./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/.store/vue-loader@15.10.1/node_modules/vue-loader/lib??vue-loader-options!./src/components/HeaderSearch/index.vue?vue&type=script&lang=js& and 29 others * core-js/modules/es.error.cause.js in ./node_modules/.store/@babel+runtime@7.22.6/node_modules/@babel/runtime/helpers/esm/regeneratorRuntime.js, ./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/.store/babel-loader@8.3.0/node_modules/babel-loader/lib!./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/.store/vue-loader@15.10.1/node_modules/vue-loader/lib??vue-loader-options!./src/layout/components/Navbar.vue?vue&type=script&lang=js& and 5 others * core-js/modules/es.object.proto.js in ./node_modules/.store/@babel+runtime@7.22.6/node_modules/@babel/runtime/helpers/esm/regeneratorRuntime.js * core-js/modules/es.regexp.dot-all.js in ./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/.store/babel-loader@8.3.0/node_modules/babel-loader/lib!./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/.store/vue-loader@15.10.1/node_modules/vue-loader/lib??vue-loader-options!./src/components/ThemePicker/index.vue?vue&type=script&lang=js&, ./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/.store/babel-loader@8.3.0/node_modules/babel-loader/lib!./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/.store/vue-loader@15.10.1/node_modules/vue-loader/lib??vue-loader-options!./src/layout/components/Navbar.vue?vue&type=script&lang=js& and 2 others * core-js/modules/web.url-search-params.delete.js in ./src/utils/request.js * core-js/modules/web.url-search-params.has.js in ./src/utils/request.js * core-js/modules/web.url-search-params.size.js in ./src/utils/request.js * qs in ./src/utils/request.js * svg-baker-runtime/browser-symbol in ./src/icons/svg/user.svg To install them, you can run: npm install --save core-js/modules/es.array.push.js core-js/modules/es.error.cause.js core-js/modules/es.object.proto.js core-js/modules/es.regexp.dot-all.js core-js/modules/web.url-search-params.delete.js core-js/modules/web.url-search-params.has.js core-js/modules/web.url-search-params.size.js qs svg-baker-runtime/browser-symbol怎么解决如何安装
最新发布
07-21

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值