첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
0.00MB

▶ Program.cs

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 합계 구하기 - GetSummary(sourceEnumerable)

        /// <summary>
        /// 합계 구하기
        /// </summary>
        /// <param name="sourceEnumerable">소스 열거 가능형</param>
        /// <returns>합계</returns>
        private static int GetSummary(IEnumerable<int> sourceEnumerable)
        {
            object mutex      = new object();
            int    totalValue = 0;

            Parallel.ForEach
            (
                source       : sourceEnumerable,
                localInit    : () => 0,
                body         : (item, state, localValue) => localValue + item,
                localFinally : localValue =>
                {
                    lock(mutex)
                    {
                        totalValue += localValue;
                    }
                }
            );

            return totalValue;
        }

        #endregion
        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            int[] valueArray = new int[] { 10, 20, 30, 20, 10, 30, 40 , 50 };

            int summrary = GetSummary(valueArray);

            Console.WriteLine(summrary);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요