Disclaimer: This blog is my experience in order to save their own learning, the content is mainly from the great God-the May Cangjie blog to learn from, in this thanks to the great god of the May Cangjie share, salute!
Chapter One: Concepts of processes and threads
Process: The process is the basic unit of allocating resources in the operating system, each running program on a computer system needs a process, and each process needs a new piece of memory, and then the operating system maps a piece of physical memory to the address space of the process;
Threads: Threads act as the unit of independent and independent dispatch, and each individual subtask in a process is a thread. The main thread of this blog (based on Java)
Here is a demo:
first Way , create a thread, inherit thread
1 Public classMyThreadextendsThread {2 Public voidrun () {3 for(inti = 0; I <5; i++) {4 Try {5Thread.Sleep (1000);6 System.out.println (Thread.CurrentThread (). GetName ());7}Catch(interruptedexception e) {8 //TODO auto-generated Catch block9 e.printstacktrace ();Ten } One } A } -}
Write a main () method
Public classTest { Public Static voidMain (string[] args) {MyThread MyThread=NewMyThread (); Mythread.start (); for(inti = 0; I <5; i++) { Try{Thread.Sleep (500); System.out.println (Thread.CurrentThread (). GetName ()); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }}
Run results
Mainmainthread-0mainthread-0mainmainthread-0thread-0thread-0
We can learn from the demo above that the threads are independent of each other, the main thread and the Thread-0 thread are running alternately, in fact, it is simultaneously running non-interference, that is, the execution of one thread does not have to wait for another thread to execute after execution, which greatly improves the utilization of the server resources.
The second, the way to create a thread is to implement the Runnable interface, see the source code we can find that actually thread also implements the Runnable interface, so after the implementation of the Runnable interface, we still need the thread two start thread, as follows:
Newnew Thread (mt0); T.start ();
Compared with the above two ways, the relative implementation of the Runnable interface is relatively better, the reason: inheritance is a single inheritance, implementation can be implemented more, and the implementation can reduce the degree of coupling between programs.
Here are the six states of threads that I summarize:
1. Newly created state new
New but not the state of the thread that started. For example, "thread t = new thread ()", and T is a thread in the new state
2. Operational status runnable
New comes out of the thread, and the call Start () method is in the runnable state. A thread in the runnable state may be running in a Java virtual machine, or it may be waiting for the processor's resources, because a thread must obtain the CPU's resources before it can run the contents of its run () method, otherwise queued
3, blocking blocked
If a thread is waiting for a monitor lock in order to enter a synchronized block/method, then the state of the threads is blocking blocked
4, Waiting for waiting
A thread is waiting for the waiting state because it calls the wait () method of the object without the timeout, the join () method of the thread without the timeout, and the Locksupport Park () method.
5, timeout waiting timed_waiting
A thread is called by the Wait () method with the object that specifies the waiting time, the join () method of the thread, the sleep () method of the thread, the Parknanos () method of the Locksupport, Locksupport Parkuntil () method, it will be in the timeout wait timed_waiting state
6. Termination status Terminated
When a thread call terminates or the run () method finishes executing, the thread is in a signaled state. The thread in the terminating state does not have the ability to continue running
Java Multithreaded Learning One