Two ways of creating threads in Java multithreading and comparative code examples
1. Concept of thread: thread refers to the execution flow of a task from beginning to end. Thread provides a mechanism for running tasks. For Java, multiple threads can be executed concurrently in a program, and these threads can run simultaneously on a multiprocessor system. When the program runs as an application, the Java interpreter starts a thread for the main () method.
2. Parallelism and Concurrency:
(1) Concurrency: in a single processor system, multiple threads share CPU time, and the operating system is responsible for scheduling and allocating resources to them.
(2) Parallelism: in a multiprocessor system, multiple processors can run multiple threads at the same time. These threads can run at the same time. Unlike concurrency, only multiple threads can share CPU time, and only one thread can run at the same time.
3. Thread creation:
(1) Basic concept: each task in Java is a runnable object. In order to create a task, you must first define a task class, which must implement the runnable interface. Threads are essentially objects that facilitate task execution. The execution process of a thread is the execution to the end of the run () method in a task class.
(2) Create a thread through the runnable interface:
A. define a task class to implement the runnable interface, implement the run () method in the runnable interface (the run () method tells the system thread how to run), and define the specific task code or processing logic in the run () method.
B. after defining the task class, create a task object for the task class.
C. the task must be executed in the thread. Create an object of the tree class, and pass the previously created task class object that implements the runnable interface as a parameter to the construction method of the tree class.
D. call the start () method of the tree class object to start a thread. It will cause the run () method of the task to be executed. When the run () method is executed, the thread will terminate.
Example code:
Program running results:
Run two task objects at the same time:
Operation results:
(3) Inherit the thread class to create a thread:
A. first, create a task class extensions thread class. Because the thread class implements the runnable interface, the user-defined task class also implements the runnable interface. Re define the run () method, which defines the specific task code or processing logic.
B. create a task class object, which can use thread or runnable as a custom variable type.
C. call the start () method of the custom object to start a thread.
Example code:
Operation results:
One thread waits for the end of another thread before executing: when executing the printnum task, when printing to the number 50, turn to the task of printing character c, and continue to execute the digital printing task until thread thread4 is completed.
4. Comparison of the two methods (Reprint)
First, analyze the output results of the two methods. Two threads are also created. Why are the results different?
Threads created by implementing the runnable interface can share the same target object (treaddemo1tt = newtraddemo1();), It realizes that multiple same threads process the same resource. When the first thread finishes executing the task, countdown is already 0, so the second thread will not output. In the way of inheriting thread to create thread, new lists two task class objects with their own member variables, which do not interfere with each other.
Then look at an explanation from JDK:
The runnable interface should be implemented by classes that intend to execute their instances through a thread. Class must define a parameterless method called run.
The interface is designed to provide a common protocol for objects that want to execute code at the time of an activity. For example, the thread class implements runnable. Activation means that a thread has started and has not stopped.
In addition, runnable provides an activation method for classes that are not thread subclasses. By instantiating a thread instance and taking itself as the running target, you can run the class that implements runnable. In most cases, if you want to override only the run () method and not the other thread methods, you should use the runnable interface. This is important because programmers should not create subclasses for a class unless they intend to modify or enhance its basic behavior. (it is recommended to create a task class and implement the runnable interface instead of inheriting the thread class)
Inherit thread class:
(1) Advantages: it is easy to write. If you need to access the current thread, you don't need to use the thread. Currentthread() method. You can directly use this to obtain the current thread.
(2) Disadvantages: because the thread class has inherited the thread class, it can no longer inherit other parent classes.
The runnable interface is implemented:
(1) Advantages: thread class only implements the runable interface and can inherit other classes. In this way, multiple threads can share the same target object, so it is very suitable for multiple same threads to process the same resource. Therefore, CPU code and data can be separated to form a clear model, which better reflects the object-oriented idea.
(2) Disadvantages: programming is slightly complicated. If you need to access the current thread, you must use the thread. Currentthread () method.
summary
The above is all about the two creation methods of threads in Java multithreading and the comparison code examples in this paper. I hope it will be helpful to you. If there are deficiencies, please leave a message to point out. Thank you for your support!