Java | multithreading (1)

The difference between threads and processes:

First, about processes and threads, first of all, from the definition of understanding is different,
1, what is the process? Has the independent function program, it is an independent unit of resource allocation and scheduling system, focus on system scheduling and individual units, that is to say the process is a program can be run independently.
2, what’s the thread? An entity of a thread process that is the basic unit of CPU scheduling and dispatching. It is the smallest unit that can run independently of a process, and threads themselves do not have system resources. At run time, just use some counters, registers and stack.
two, the relationship between them
1, a thread can only belong to a process, and a process can have more than one thread, but at least one thread (usually the main thread).
2, the resources are assigned to the process, and all threads of the same process share all the resources of the process.
3, threads need collaboration synchronization during execution. Threads between different processes use message communication to synchronize.
4, the processor divides threads, which are threads running on the processor. 5, thread refers to an execution unit within the process, as well as a process entity within the process.
three analyzes the differences between the two from three perspectives.
1. Scheduling: threads are the basic unit of scheduling and allocation, and processes are the basic unit of resources.
2 and Concurrency: not only can processes be executed concurrently, but concurrently between multiple threads of the same process can be executed concurrently.
3 has the resources: the process is an independent unit of resources, the thread does not have system resources, but access to resources belonging to the process,
thread is a set of instructions.
Author: Durian Kim Jongwoon
link: https://www.zhihu.com/question/21535820/answer

How to create multithreaded:

1. inherit the Thread class

/ * * * * @ClassName: MyThread2 * @Description: TODO (through inheritance Thread multi-threaded) * @author huasc * @date * 9:20:04 * public class August 29, 2017 morning MyThread2 extends Thread run @Override public {/ / rewriting method (void run) {for (int i = 0; I 10; < i++) {/ / System.out.println (output thread this.getName (+i));}}} (super.run);

2. implement the Runable interface

/ * * * * @ClassName: MyThread * @Description: TODO (by implementing the Runnable interface to create multiple threads) * @author huasc * @date * 9:16:42 * public class August 29, 2017 morning MyThread implements Runnable run @Override public {/ / rewriting method (void run) {for (int i = 0; I 10; < i++) {/ / output thread System.out.println (Thread.currentThread) (.GetName) (+i);}}}

3. is used directly in the body of the function:

/ * * * * @ClassName: MyThread3 * @Description: TODO (multi thread created directly in the function body) * @author huasc * @date * 9:25:37 * public class August 29, 2017 morning MyThread3 thread Thread {Thread = new (new (Runnable) @Override public void (run) {{for (int i = 0; I 10; < i++) {/ / output thread System.out.println (Thread.currentThread) (.GetName) (+i);}}}});

compare

The advantage of implementing the Runnable interface:
1) for the same code thread to handle the same resource
2) can avoid the limit of
Java in the Dan Jicheng 3) increase the robustness of the program, the code can be shared by multiple threads, code and data independence.
inherits the Thread class advantage:
1) that abstracts the thread class when you need to design it in an abstract factory pattern.
2) multithreaded synchronous
uses the advantage
1 in the body of functions. There is no need to inherit thread or implement Runnable to reduce the scope of action.

Thread startup:

Public class MyThreadTest static void main {public (String[] args) {/ / Thread class inheritance starting new (MyThread2).Start (New); MyThread2 (.Start) (New) (MyThread2); (.Start); / / Runnable interface MyThread m MyThread startup mode = new (new (m, Thread); "1: (.Start) thread"); new Thread (m, "2: thread"); new (.Start) Thread (m, "3: thread") (.Start);}}

Is actually called start threads start () method; instead of calling run (
); (run); method is executed threads, thread does not execute concurrently, but after waiting for a thread before the end of a thread to execute. Start (). The method will synchronize threads, compete for CPU resources, grab CPU execution rights, execute them, and continue without waiting

Java | multithreading (1)

Five states of a thread:

Five states of Java | multithreading (1)
thread.Png

1, new state: thread object has been created, and start () method has not been called on it.
2: ready state: when the thread is eligible to run, but the scheduler has not selected it as the thread running, the thread is in the state. When the start () method calls, the thread first enters the executable state. After the thread runs, or returns from congestion, wait, or sleep, it also returns to the operational state.
3, running state: thread scheduler from a running pool to select a thread as the current thread when the thread is in the state. This is also the only way threads get into the running state.
4, wait / block / sleep status: This is where the thread is qualified to run when it is in. In fact, the combination of the three states is one of the following: the thread is still alive, but it is not running at present. In other words, it is executable, but if an event appears, he may return to the executable state.
5, dead state: when the thread’s run () method is completed, it is considered dead. This thread object may be alive, but it is no longer a thread that is executed separately. Once the thread is dead, it cannot be resurrected. If a start () method is raised on a dead thread, the java.lang.IllegalThreadStateException exception is thrown.


(to be continued)