c# 多線程實現和獲取方法中的對應的地址

        爲了檢測多線程中同時調用一個方法,方法中的局部變量是否會替換的測試,結果爲:同一個子線程的變量的地址一直是一致的,不同的子線程中的變量的地址是不一致的,代碼如下:

/*********************************************
 * CLR 版本:       4.0.30319.42000
 * 類 名 稱:       Program
 * 機器名稱:       MS-20170310FLQY
 * 命名空間:       Choj
 * 文 件 名:       Program
 * 創建時間:       2017-09-03 16:20:48
 * 作    者:       Choj
 * 說    明:     
 * 修改時間:
 * 修 改 人:
 * 
*********************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
using System.Threading;

namespace Choj
{
    class Program
    {
        static void Main(string[] args)
        {
            new Test().Init();
        }
    }
    public class Test
    {
        public void Init()
        {
            for (int i = 0; i < 10; i++)
            {
                Thread thread = new Thread(Test1);
                thread.Start((object)i);
            }
            Console.ReadKey();
        }
        /// <summary>
        /// 
        /// </summary>
        public void Test1(object i)
        {
            for (int j = 0; j < 100; j++)
            {
                int a = j;

                unsafe
                {
                    Console.WriteLine();
                    Console.WriteLine(string.Format("i:{0}->*(&a)->(int)&a:{2}", i, *(&a), (int)&a));
                    Console.WriteLine();

                }
            }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章