ต้นฉบับ : http://www.codeproject.com/threads/ThreadClass.asp
Java
- Code: Select all
// define class with a threadable method
public class MyObject implements Runnable {
// the thread procedure
public void run() {
// TODO: put the code here
}
}
MyObject obj = new MyObject();
Thread thread = new Thread(obj);
tread.start();
.net
- Code: Select all
// define class with a threadable method
public class MyObject {
// the thread procedure
public void Run() {
// TODO: put the code here
}
}
MyObject obj = new MyObject();
Thread thread = new Thread(new ThreadStart(obj.Run));
tread.Start();
ข้อแตกต่างระหว่าง .net กับ java คงเป็นที่ object ของ .net เป็น thread อยู่แล้วกระมัง สามารถเรียกใช้ function ย่อยได้เลย ดังนั้น เราสามารถกำหนด thread ว่าให้รันใน procedure ไหนของ object ได้โดยที่ไม่ต้องสร้าง class แยก ต่างจากจาวา หากต้องการสร้าง procedure ที่จะให้รัน ต้องกำหนดเป็น if then elf หรือ สร้างเป็นคลาสแยกไป ดูตัวอย่างด้านล่างดีก่า
.net
- Code: Select all
// create a threadable object
public class MyObject {
// first thread procedure
public void ThreadProc1() {
// TODO:
}
// second thread procedure
public void ThreadProc2() {
// TODO:
}
}
MyObject obj = new MyObject();
// create first thread
Thread thread1 = new Thread( new ThreadStart(obj.ThreadProc1) );
thread1.Start();
//create second thread
Thread thread2 = new Thread( new ThreadStart(obj.ThreadProc2) );
thread2.Start();
มันจะสะดวกสะบายอะไรเช่นนี้นี่ .net (แต่เขียนไม่เป็นง่ะ ไว้ต้องหัดเขียนมั่งและ)
มาดู C++ มั่งดีกว่า เพื่อให้ง่ายสร้าง Interface แบบ Java ก่อน (แต่ C++ ไม่มี Interface หนิ ก็สร้างเป็น คลาสเลยละกัน)
- Code: Select all
// define the interface
struct IRunnable {
virtual void run() = 0;
};
// define the thread class
class Thread {
public:
Thread(IRunnable *ptr) {
_threadObj = ptr;
}
void start() {
// use the Win32 API here
DWORD threadID;
::CreateThread(0, 0, threadProc, _threadObj, 0, &threadID);
}
protected:
// Win32 compatible thread parameter and procedure
IRunnable *_threadObj;
static unsigned long __stdcall threadProc(void* ptr) {
((IRunnable*)ptr)->run();
return 0;
}
};
จากนั้นเวลาใช้ก็ตามด้านล่าง
- Code: Select all
// define class with a threadable method
class MyObject : IRunnable {
public:
// the thread procedure
virtual void run() {
// TODO: put the code here
}
}
MyObject *obj = new MyObject();
Thread thread = new Thread(obj);
tread->start();
ถ้าจะทำแบบ .net
- Code: Select all
// define class with a threadable method
class MyObject : IRunnable {
// pointer to a class method
typedef void (MyObject::* PROC)();
PROC fp;
// first thread procedure
void threadProc1() {
//TODO: code for this thread procedure
}
// second thread procedure
void threadProc2() {
//TODO: code for this thread procedure
}
public:
MyObject() {
fp = threadProc1;
}
void setThreadProc(int n) {
if(n == 1)
fp = threadProc1;
else
if(n == 2)
fp = threadProc2;
}
// the thread procedure
virtual void run() {
(this->*fp)();
}
};
MyObject *obj = new MyObject();
obj->setThreadProc(1);
Thread *thread1 = new Thread(obj);
thread1->start();
obj->setThreadProc(2);
Thread *thread2 = new Thread(obj);
thread2->start();