First, the concept of the operating system thread and process
the operating system now is a multitasking operating system. Multithreading is a way to achieve multitasking.
A process is an application running in memory in which each process has its own separate piece of memory, and a process can start multiple threads. For example , in a Windows system, a running EXE is a process.
A thread is an execution process in a process that can run multiple threads in a process. For example , a java.exe process can run many threads. Threads are always part of a process, and multiple threads in a process share the memory of the process.
" at the same time " execution is a human feeling, which actually rotates between threads.
Second, threads in Java
Use the Java.lang.Thread class or the Java.lang.Runnable interface to write code to define, instantiate, and start new threads.
A Thread class instance is just an object that, like any other object in Java, has variables and methods that live and die on the heap.
in Java, each thread has a call stack, and the thread runs in the background even if no new threads are created in the program.
A Java application always runs from the Main () method, and themian () method runs inside a thread, which is called the main thread.
Once a new thread is created, a new call stack is generated.
Threads are divided into two categories: the user thread and the waiting thread.
theJVM shuts down automatically when all user threads have finished executing . But the waiting thread is not independent of the JVM, and the waiting thread is typically created by the operating system or the user himself.
Third, the state of the Java thread
Java threads can only be in the following six states at some point
1. New, the state that the thread has not started.
2. Running (RUNNABLE), a running thread is executing in a Java virtual machine, but it may be waiting for additional resources from the operating system (such as the processor).
3. In blocking (BLOCKED), a thread attempts to acquire a monitor lock (what the JVM specification implements, each object and class is logically associated with a monitor), but the lock is in the state of being held by another thread. For example, entering a synchronize block or other thread thread calls the Object.wait method.
4. Wait (waiting), a thread invokes the state after object.wait, Thread.Join, or Locksupport.park. For example, a thread called object.wait waits for the Object.notify or Object.notifyall method.
5. Timing Wait (timed_waiting) is the state of the time parameter, which is the relevant method that triggers the wait state.
6. Terminate (TERMINATED), thread is terminated (throws an uncaught exception) or normal exit state.
Four, Start Thread
There are two ways to speak at the language level. An instance of the Java.lang.Thread class is a thread but it needs to invoke the Java.lang.Runnable interface to execute, since the thread class itself is the runnable interface of the call so you can inherit Java.lang.Thread Class or call the Runnable interface directly to override the run () method to implement the thread
Call the Start () method on the thread object of the thread , not run () or any other method.
Before the start () method is called: The thread is in the new state, the new state refers to a thread object, but there is no real thread.
After calling the start () method: A series of complex things happened;
Start a new execution thread (with a new call stack);
The thread is transferred from the new state to the operational state;
When the thread obtains an opportunity to execute, its target run () method runs.
The start () method is used to start the newly created thread, and start () calls the run () method internally, which is not the same as calling the run () method directly. When you call the run () method, only the original thread is called, no new thread is started, and the start () method starts the new thread.
Note: for javarun () main () like, it's just a new thread that knows the method name of the call ( Span style= "Color:black" and signature runnableThreadrun
v. race condition in Java
六、一个 thread Runtime exception
If the exception is not captured the thread will stop executing. Thread.uncaughtexceptionhandler is an inline interface for dealing with abrupt interruptions of threads caused by uncaught exceptions. When an uncaught exception causes the thread to break, the JVM uses Thread.getuncaughtexceptionhandler () To query the thread's Uncaughtexceptionhandler and pass the thread and exception as arguments to the handler's Uncaughtexception () method for processing.
vii. java thread deadlock
Deadlock deadlock in Java multithreading refers to two or more two processes in the execution process, because of contention for resources caused by a mutual waiting phenomenon, if there is no external force, they will not be able to proceed. This is a serious problem because a deadlock will cause your program to hang and cannot complete the task, the following four conditions must be met for a deadlock to occur:
- Mutex condition: A resource can only be used by one process at a time.
- Request and hold condition: When a process is blocked by a request for resources, it remains in place for the resources that have been obtained.
- Non-deprivation: the resources that the process has acquired cannot be forcibly stripped until the end of use.
- Cyclic wait condition: a cyclic waiting resource relationship is formed between several processes.
The simplest way to avoid deadlocks is to stop the loop waiting condition, set the flags, sort all the resources in the system, and stipulate that all process request resources must operate in a certain order (ascending or descending) to avoid deadlocks.
Viii. Synchronization of Threads
Identifies a variable in the resource wrapper class that is competing for access to be changed as private;
Synchronize the code that modifies the variable, using the Synchronized keyword synchronization method or code.
Synchronization Method
PublicSynchronizedFix int (IntY) {
x = x-Y
SYSTEM.OUT.PRINTLN ("Thread" +thread.currentthread (). GetName () + "run End, decrease" "+Y
+ "", the current value is: "+ x);
return x;
}
For lock and Sync, here are a few points:
1), can only synchronize methods, but not synchronization variables and classes;
2), each object has only one lock; when it comes to synchronization, what should be clearly synchronized? In other words, on which object is it synchronized?
3), you do not have to synchronize all the methods in the class, the class can have both synchronous and non-synchronous methods.
4), if two threads are going to execute the synchronized method in a class, and two threads use the same instance to invoke the method, only one thread can execute the method at a time, and the other waits until the lock is freed. That is, if a thread obtains a lock on the object, no other thread can enter any one of the synchronization methods in the class (the object).
5), if the thread has synchronous and non-synchronous methods, the non-synchronous method can be freely accessed by multiple threads without being restricted by the lock.
6), when the thread sleeps, any locks it holds will not be released.
7), threads can obtain multiple locks. For example, a synchronous method that invokes another object in a synchronous method of an object acquires a synchronization lock for two objects.
8), synchronization damage concurrency, should be as narrow as possible synchronization range. Synchronization can not only synchronize the entire method, but also synchronize some of the code blocks in the method.
9), when using synchronous code blocks, you should specify which object to synchronize on, that is, which object to get the lock.
Java Threading Learning.