-
package main
import (
"fmt"
"math"
)
func RunRoutine(f int, theStart int, theEnd int) {
fmt.Printf("~ 本次线程参数:线程index=%d,本次开始=%d,本次结束=%d \n", f, theStart, theEnd)
for l:= theStart; l< theEnd; l++ {
//time.Sleep(123 * time.Millisecond)
fmt.Printf("@ 线程内循环:线程index=%d,内循环index=%d \n", f, l)
// 其他操作
}
}
func Test() {
goNum := 20 // 多线程数量
start := 3433 // 开始
end := 4100 // 结束
// 固定参数范围
num := end-start
if num < 0 {
end = start
goNum = 1
}else if num < goNum {
goNum = end-start
}
theFoot := math.Floor(float64((end-start) / goNum)) // 区间步长
// 处理主要
for f:=0; f<goNum; f++ { // 多线程处理
fmt.Printf("> 线程数量进度=%d/%d \n", f, goNum)
theStart := float64(start) + theFoot*float64(f)
theEnd := float64(start) + theFoot*float64(f+1)
go RunRoutine(f, int(theStart), int(theEnd)) // 开启多线程
}
// 处理剩余
theStart := start + int(theFoot)*goNum
theEnd := end
fmt.Printf("> 线程数量进度=%d完成,剩余数量=%d \n", goNum, theEnd - theStart)
RunRoutine(goNum, theStart, theEnd)
}
func main() {
Test()
}
-