Processes and Threads
Process: Each process has its own code and data space (process context), and there is a significant overhead of switching between processes, and a process contains 1--n threads.
Threads: The same class of threads share code and data space, each thread has a separate run stack and program counter (PC), and thread switching overhead is small.
1.1 Multi-threaded vs. Multi-process
Multi-process means that the operating system can run multiple tasks (programs) at the same time.
Multithreading refers to the execution of multiple sequential streams in the same program.
1.2 State transitions for threads
1. New state: The thread object has been created and the start () method has not been invoked on it.
2. Operational status: When the thread is eligible to run, but the scheduler has not selected it as the state in which the thread is running. When the start () method is called, the thread first enters the operational state. After the thread has run or has returned from blocking, waiting, or sleeping, it returns to the operational state.
3. Running state: The thread Scheduler selects a thread from a running pool as the current thread is in the state of the thread. This is also the only way that a thread goes into a running state.
4. Wait/block/Sleep state: This is the state at which the thread is eligible to run. In fact, this three-state combination is one, and its common denominator is that the thread is still alive, but there is no condition to run at the moment. In other words, it is operational, but if an event occurs, he may return to the operational state.
5. Death state: When the thread's run () method finishes, it is considered dead. This thread object may be alive, but it is not a separate thread. Once a thread dies, it cannot be resurrected. If you call the start () method on a dead thread, the java.lang.IllegalThreadStateException exception is thrown.
1.3 Ways to create threads in Java
There are two ways to create threads in Java: 1. Inherit the thread class; 2. Implement Runnable interface
PackageCom.sgl.thread.demo; Public classMythreadextendsThread {Private intnum; PrivateString Tname; PublicString Gettname () {returnTname; } Public voidsettname (String tname) { This. Tname =Tname; } Public intGetnum () {returnnum; } Public voidSetnum (intnum) { This. num =num; } PublicMythread (intnum, String tname) { This. num =num; This. Tname =Tname; } @Override Public voidrun () { while(Num < 100) {System.err.println ("Thread:" + Tname + "--" + num++); } } Public Static voidMain (string[] args) {Mythread mythread1=NewMythread (1, "Thread 1"); Mythread mythread2=NewMythread (1, "Thread 2"); Mythread1.start (); Mythread2.start (); }}
PackageCom.sgl.thread.demo; Public classMyrunnableImplementsRunnable {Private intNum=0; @Override Public voidrun () { while(num<100) {System.out.println (num++); } } Public Static voidMain (string[] args) {myrunnable myrunnable=Newmyrunnable (); Thread Thread=NewThread (myrunnable); Thread.Start (); }}1.4JAVA Thread Memory model
Interaction diagram between threads, working memory, and main memory:
All threads share main memory
Each thread has its own working memory
Java Basics---threads and processes