• Go_runtime包


    package main
    
    import (
    	"fmt"
    	"runtime"
    	"time"
    )
    
    //写在init函数里,main函数运行之前就先设置cpu
    func init() {
    	//获取逻辑cpu的数量
    	fmt.Println("逻辑CPU的数量-->", runtime.NumCPU()) //逻辑CPU的数量--> 12
    
    	//设置go程序执行的最大的cpu的数量:数值在[1,256]
    	n := runtime.GOMAXPROCS(runtime.NumCPU())
    	fmt.Println(n) //12
    }
    func main() {
    	//获取goroot目录
    	fmt.Println("GOROOT-->", runtime.GOROOT()) //C:Go
    	//获取操作系统
    	fmt.Println("os/platform-->", runtime.GOOS) //dos/platform--> windows
    
    	//gosched 让当前线程让出 `cpu` 以让其它线程运行,它不会挂起当前线程,因此当前线程未来会继续执行
    	go func(){
    		for i:=0;i<5;i++{
    			fmt.Println("goroutine...")
    		}
    	}()
    	for i:=0;i<4;i++{
    		//让出时间片,先让别的goroutine执行
    		runtime.Gosched()
    		fmt.Println("main...")
    	}
    
    	//创建goroutine
    	go func() {
    		fmt.Println("goroutine开始。。")
    		//调用fun
    		fun()
    		fmt.Println("goroutine结束。。。")
    	}()
    
    	//睡一会儿
    	time.Sleep(3 * time.Second)
    
    }
    
    func fun() {
    	defer fmt.Println("derfer...")
    	//return //终止函数
    	runtime.Goexit() //终止当前的goroutine
    	fmt.Println("fun函数。。。")
    }
    

      

  • 相关阅读:
    struts2 文件上传
    struts2 前端显示错误信息
    struts2
    struts2 针对类型转换出错的处理
    Beats:使用filebeat传送多行日志multiline
    logstash的output插件
    logstash的mutate插件
    logstash中date的时间处理方式总结
    logstash更新gem源
    logstash的Multiline插件
  • 原文地址:https://www.cnblogs.com/yzg-14/p/12251796.html
Copyright © 2020-2023  润新知