您的位置:首页 > 移动开发 > Objective-C

Thread.SetData 方法

2008-04-09 14:51 330 查看
Thread.SetData 方法

在当前正在运行的线程上为此线程的当前域在指定槽中设置数据。

命名空间:System.Threading
程序集:mscorlib(在 mscorlib.dll 中)


 语法
C#
public static void SetData (
LocalDataStoreSlot slot,
Object data
)


C++

public:
static void SetData (
LocalDataStoreSlot^ slot,
Object^ data
)

参数

slot

在其中设置值的 LocalDataStoreSlot

data

要设置的值。
C#


using System;


using System.Threading;




class Test




...{


    static void Main()




    ...{


        Thread[] newThreads = new Thread[4];


        for(int i = 0; i < newThreads.Length; i++)




        ...{


            newThreads[i] = 


                new Thread(new ThreadStart(Slot.SlotTest));


            newThreads[i].Start();


        }


    }


}




class Slot




...{


    static Random randomGenerator = new Random();




    public static void SlotTest()




    ...{


        // Set different data in each thread's data slot.


        Thread.SetData(


            Thread.GetNamedDataSlot("Random"), 


            randomGenerator.Next(1, 200));




        // Write the data from each thread's data slot.


        Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", 


            AppDomain.GetCurrentThreadId().ToString(),


            Thread.GetData(


            Thread.GetNamedDataSlot("Random")).ToString());




        // Allow other threads time to execute SetData to show


        // that a thread's data slot is unique to the thread.


        Thread.Sleep(1000);




        Console.WriteLine("Data in thread_{0}'s data slot is still: {1,3}", 


            AppDomain.GetCurrentThreadId().ToString(),


            Thread.GetData(


            Thread.GetNamedDataSlot("Random")).ToString());




        // Allow time for other threads to show their data,


        // then demonstrate that any code a thread executes


        // has access to the thread's named data slot.        


        Thread.Sleep(1000);




        Other o = new Other();


        o.ShowSlotData();


    }


}




public class Other




...{


    public void ShowSlotData()




    ...{


        // This method has no access to the data in the Slot


        // class, but when executed by a thread it can obtain


        // the thread's data from a named slot.


        Console.WriteLine("Other code displays data in thread_{0}'s data slot: {1,3}", 


            AppDomain.GetCurrentThreadId().ToString(), 


            Thread.GetData( 


            Thread.GetNamedDataSlot("Random")).ToString());


    }



C++



using namespace System;


using namespace System::Threading;


ref class Other




...{


public:


   void ShowSlotData()




   ...{


      


      // This method has no access to the data in the Slot


      // class, but when executed by a thread it can obtain


      // the thread's data from a named slot.


      Console::WriteLine(  "Other code displays data in thread_{0}'s data slot: {1,3}", AppDomain::GetCurrentThreadId(), Thread::GetData( Thread::GetNamedDataSlot(  "Random" ) )->ToString() );


   }




};




ref class Slot




...{


private:


   static Random^ randomGenerator = gcnew Random;




public:


   static void SlotTest()




   ...{


      


      // Set different data in each thread's data slot.


      Thread::SetData( Thread::GetNamedDataSlot( "Random" ), randomGenerator->Next( 1, 200 ) );


      


      // Write the data from each thread's data slot.


      Console::WriteLine( "Data in thread_{0}'s data slot: {1,3}", AppDomain::GetCurrentThreadId().ToString(), Thread::GetData( Thread::GetNamedDataSlot( "Random" ) )->ToString() );


      


      // Allow other threads time to execute SetData to show


      // that a thread's data slot is unique to the thread.


      Thread::Sleep( 1000 );


      Console::WriteLine( "Data in thread_{0}'s data slot is still: {1,3}", AppDomain::GetCurrentThreadId().ToString(), Thread::GetData( Thread::GetNamedDataSlot( "Random" ) )->ToString() );


      


      // Allow time for other threads to show their data,


      // then demonstrate that any code a thread executes


      // has access to the thread's named data slot.        


      Thread::Sleep( 1000 );


      Other^ o = gcnew Other;


      o->ShowSlotData();


   }




};




int main()




...{


   array<Thread^>^newThreads = gcnew array<Thread^>(4);


   for ( int i = 0; i < newThreads->Length; i++ )




   ...{


      newThreads[ i ] = gcnew Thread( gcnew ThreadStart( &Slot::SlotTest ) );


      newThreads[ i ]->Start();




   }


}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息