码迷,mamicode.com
首页 > Web开发 > 详细

Go与.Net TAP

时间:2017-09-17 01:24:12      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:static   system   oid   sha   queue   console   pac   ring   ati   

Go

package main

import "fmt"

func sum(arrays []int, ch chan int) {
    fmt.Println(arrays)
    sum := 0
    for _, array := range arrays {
        sum += array
    }
    ch <- sum
}

func main() {
    arrayChan := make(chan int, 20)
    arrayInt := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
    for t := 0; t < 10; t++ {
        length := len(arrayInt)
        go sum(arrayInt[length-t:], arrayChan)
    }

    arrayResult := [10]int{0}
    for i := 0; i < 10; i++ {
        arrayResult[i] = <-arrayChan
    }
    fmt.Println(arrayResult)
}

  C#

class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            System.Collections.Concurrent.ConcurrentQueue<int> result = new System.Collections.Concurrent.ConcurrentQueue<int>();


            var r = Parallel.For(0, 10, i =>
              {
                  var index = arr.Length - i;
                  int[] arr1 = new int[i];
                  Array.Copy(arr, index, arr1, 0, i);
                  result.Enqueue(Sum(arr1));
              }
           );           

            foreach (var a in result)
            {
                System.Console.Write(a);
                System.Console.Write(" ");
            }
            System.Console.ReadLine();
        }

        static int Sum(int[] array)
        {
            int sum = 0;
            foreach (var i in array)
            {
                sum += i;
            }
            return sum;
        }
    }

  

Go与.Net TAP

标签:static   system   oid   sha   queue   console   pac   ring   ati   

原文地址:http://www.cnblogs.com/icoolno1/p/7533600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!