
class MyClass
{
private Thread _thread
private void MyThreadStartMethod(){...}
private void StartThread()
{
_thread = new Thread(new ThreadStart(MyThreadStartMethod));
_thread.Name = "An Appropriate Name"; // Good Practice
_thread.start()
}
}
In Java we have 2 ways to create a thread. The *easy* (but not recommended) way to create a thread
is to derive from the thread class and override the run method. The beat way is to create a class
that implements Runnable (an interface with a single parameterless method called run() ) and pass that
to a new instance of a Thread instance. Both ways then require the start method to be called.
class MyThreadClass extends Thread
{
public void run() { . . . }
}
class MyRunnableClass implements Runnable
{
public void run() { . . . }
}
class MyTestClass
{
private MyThreadClass _thread1;
private Thread _thread2;
private void FirstWay()
{
_thread1 = new MyThreadClass();
_thread1.setName("An Appropriate Name"); // Good Practice
_thread1.start()
}
private void SecondWay()
{
_thread2 = new Thread(new MyRunnableClass());
_thread2.setName("An Appropriate Name"); // Good Practice
_thread2.start()
}
}
In WIN32 A thread is not an object, but a handle. There are also 3 ways to create a thread, namely;
private void CreateAndStartAThread()
{
unsigned threadId;
uintptr_t hThread = _beginthreadex(
NULL, // Ignoring Security
0 , // Default Stack Size
&ThreadProc,// Function to call
NULL, // void* Arg List
0, // 0 for running or CREATE_SUSPENDED for suspended
&threadId // Thread ID from OS
CloseHandle(hThread);// Remember you MUST close this when you are done with this thread
}
private static unsigned ThreadProc(void* pArgs)
{
// Must be static to avoid the 'this' pointer becoming a parameter
}
Next >>
Privacy Policy
©2008 DebugInspector. All rights reserved