Java, C # provides an object-oriented thread model. They all abstract thread objects, while developers implement thread application logic in a member of some classes. The development of thread application logic is simplified by separating thread objects and thread methods.
There are two ways to develop threads in Java. The first method is to derive a thread class in Thread, implement the RUN method, and call the thread.start method to start the thread, such as:
Class Mythread Extends Thread {// Create a thread class
Public void Run () {...} // thread method
}
Mythread Athread = new mythread (); // Create a thread object
Athread.Start (); // Start thread
The second method is to create a executable class by implementing the Runable interface, and use a Thread object to start the thread, such as:
Class MyRunable Implements Runnable {
Public void Run () {...} // thread method
}
Myrunable arunable = new myrunable (); // Create an executable object
Thread Athread = New Thread (arunable); // Create a thread object and associate with the executable object
Athread.Start (); // Start thread
The thread model of the C # separates the thread object and the thread method more thoroughly, it can use any prototype to void () a public class member method (static or non-static) as a thread method; the thread is also specified for a thread when the thread is started. The object of the method (the object provides various information required for thread application logic). Here is a simple example:
Using system;
Using system.threading;
Public class threadwork {// threadwork does not explicitly inherit any class, DOWORK can act as a thread method
Public void doork () {for (int i = 0; i <10; i ) Console.Writeline ("Working Thread ...");} // end of demork
} // end of threadwork
Class threadtest {
Public static void main () {// Execute the DOWORK method of the object ATHREAD as a thread method
ThreadWork Athread = new threadwork ();
ThreadStart mythreadDelegate = new threadstart (athread.dowork); // Specify thread method
Thread mythread = new thread (mythreaddelegate); // Create a thread object
mythread.start (); // Start thread
} // end of
Main
} // end of threadtest
The developer is most concerned about how to implement a thread method.
Java
Thread models provide two ways to implement thread methods, overload
Runable.run
Method or overload
Thread.run
method. Developers can choose the right base class according to specific applications (
Runable
or
Thread
),this is
Java
One advantage of thread model. You can see it,
Java
Only in the thread model
Runable
or
Thread
Thread method in subclass (ie,
Run
Methods), and each subclass can only implement a thread method.
C #
The thread model is allowed to be Void ()
The public class (static or non-static) is used as a thread method, so it actually allows the thread method in any class (not required to be a subclass of a class), and can be implemented in the same class Multiple thread methods. Object-oriented thread models have two elements: thread objects and thread methods. Thread object package thread itself related logic, such as thread creation, destruction, etc. The thread method is the task actually implemented by thread objects. When using the object-oriented thread model for multi-thread development, this will think more naturally: Now I have a job (ie, a thread method) is designed to find a worker (thread object) to complete this work. So we think
C #
The thread model is more flexible, and this flexibility enables developers to be more clear and reasonable.