我的main入口是这样的
func main() {
//创建消息通道
var quitMsg chan int = make(chan int)
//数据量
var num = 100000
//生成随机数组
var randomArray []int = GeneralRandomArray(num,0,num)
//生成近似随机数组
var nearlyArray []int = GeneralNearlyArray(num,700)
go func() { //起一个协程
for i := 0; i < 3;i ++ {
TestAltorithm(randomArray,false,num,i) //循环测试不同算法
}
quitMsg <- 0 //channel 存入消息 保证不销毁
}()
<- quitMsg //消费 消息 销毁
}
java 有点麻烦,用原生写的
public static void main(String[] args) throws InterruptedException , ExecutionException {
int num = 10000;
int arr[] = GeneralRandomArray.initLimitRandomArray(num,0,num);//生成测试数组
System.out.println("数据量"+num);
//开启四个线程
ExecutorService threadPool = Executors.newFixedThreadPool(4);
for (int i = 0;i < 4;i ++) {
final int TaskId = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
if (TaskId == 0) {
TestAlgorithm(arr,false,num,AlgorithmTyep.InsertSort);
} else if (TaskId == 1) {
TestAlgorithm(arr,false,num,AlgorithmTyep.SelectSort);
}else if (TaskId == 2){
TestAlgorithm(arr,false,num,AlgorithmTyep.MergeSort);
}else {
threadPool.shutdown();
}
}
});
}
}
dispatch_queue_t queue = dispatch_queue_create("CONCURRENT", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(dispatch_get_main_queue(), ^{
//执行队列应该为 DISPATCH_QUEUE_CONCURRENT
//但是 在非主线程会直接销毁
//所以还是用主队列吧
});
Swift5.5 新出了async 和 await
暂时先不用,国内企业用Swift版本一般不会太高
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。