We all know that there are two ways to implement multithreading: 1. inherit from Thread class 2. Implement the runnable interface
The former is easy to understand. A simple way to possess a certain nature is to inherit it. For example, if you want to be rich, the easiest way is to say that your father is Li Gang!
Java is a single inheritance, but this method wastes the only valuable opportunity for inheritance, which is not cost-effective. So the second implementation
Multi-threaded method-implement the runnable interface. This method is not so straightforward. Why is it so amazing?
What design concepts are included in this project? What is the principle behind this? My thoughts and understandings are as follows:
The thread class is like a factory. He can make furniture. If we want to make furniture, there are two ways:
First, I learned to build the same factory and create furniture in my factory. This is equivalent to inheriting our class to the thread and creating multiple threads.
Second, we don't need to open a factory by ourselves. Let the thread factory process it for us. The factory posted a notice saying that a kind of wood called runnable can be processed (the thread constructor can receive the runnable parameter ),
Then we took our runnable wood to the factory to process and make furniture. I call this method a generation processing mode. Is this really nice? Furniture you want, help others
Made. You can choose to open another factory (inherit other classes ). The precondition for the generation processing is that the thread factory can process runnable wood, and you can also process it by the thread factory.
Runnable wood.
Sample Code for two ways to create a thread:
/* Thread Creation Method 1: inherits the Thread class. Step: 1. Define a class to inherit the Thread class. 2. overwrite the run method in the Thread class. 3. directly create a thread subclass object to create a thread. 4. Call the start method to enable the thread and call the run method of the thread. You can get the thread name through the getname of the thread (starting from 0). The main thread name is main. */Class demo extends thread {private string name; demo (string name) {super (name); // This. name = Name;} public void run () {for (INT x = 0; x <10; X ++) {// For (INT y =-9999999; y< 999999999; y ++) {} system. out. println (name + ".... X = "+ x + "..... name = "+ thread. currentthread (). getname () ;}} class threaddemo2 {public static void main (string [] ARGs) {/* Create a thread to enable an execution path, run the Specified Code and other code implementations simultaneously. The specified code is the task of executing the path. Tasks of the main thread created by JVM are defined in the main function. Where is the job of a custom thread? The thread class is used to describe the thread and the thread needs tasks. So the Thread class also describes the task. This task is reflected by the run method in the Thread class. That is to say, the run method is a function that encapsulates a custom thread to run a task. The run method defines the task code to be run by the thread. The thread is enabled to run the Specified Code. Therefore, only the Thread class is inherited and the run method is rewritten. Define the Running code in the run method. * /// Thread T1 = new thread (); demo d1 = new demo ("wangcai"); demo D2 = new demo ("Xiaoqiang"); d1.start (); // enable the thread and call the run method. D2.start (); system. out. println ("over .... "+ thread. currentthread (). getname () ;}// what is the difference between calling run and calling start? Package test;/*** thread creation method 2. runnable interface implementation * requirement: simple ticket selling program, multi-window ticket selling * steps: * 1. Define the class to implement the runnbale interface * 2. Override the run method. Put the method to run multiple threads into the run method. * 3. Create a multi-threaded object through the Thread class. * 4. Pass the sub-object of the runnable interface as the actual parameter to the Thread class * 5. Call the start method of the thread class to enable the thread (the run method will be called by yourself). ** the advantages of the runnable method: * 1. Solve the problem that Java cannot inherit more. * 2. Pass the ticket resource as a shared resource to multiple threads. */Public class runnabletest {public static void main (string [] ARGs) {ticket T = new ticket (); thread T1 = new thread (t ); thread t2 = new thread (t); thread T3 = new thread (t); thread t4 = new thread (t); t1.start (); t2.start (); t3.start (); t4.start () ;}} class ticket implements runnable {private int ticket = 100; Public void run () {While (true) {If (ticket> 0) {system. out. println (thread. currentthread (). getname () + "buy:" + ticket --);}}}}}