I. What is multi-threaded
There are two concepts to understand before learning a multi-process:
Process: Each process has a separate code and data space (process context), the transition between processes will have a large cost, a process contains 1--n threads, the process is the smallest unit of resource allocation;
Threads: The same class of threads share code and data space, each thread has a separate run stack and program counter (PC), thread switching overhead is small, the thread is the smallest unit of CPU scheduling;
Threads and processes are divided into five phases: Create, ready, run, block, terminate.
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.
Multithreading is a special form of multitasking, but multithreading uses a much smaller resource overhead.
Here you define another term related to threading-process: a process that includes the memory space allocated by the operating system and contains one or more threads. A thread cannot exist independently, it must be part of a process. A process runs until all non-daemon threads have finished running before the end. Multithreading can satisfy programmers to write high-efficiency programs to achieve the full use of CPU.
Two. How to turn on multithreading
In Java to implement multi-threading, there are two ways, one is to inherit the thread class, and the other is to implement the Runable interface.
Implement the Runable interface:
To implement Runnable, a class only needs to execute a method call run (), after creating a class that implements the Runnable interface, you can instantiate a thread object in the class, and after the new thread is created, you call its start () method before it runs.
Inherit the thread class:
The second way to create a thread is to create a new class that inherits the thread class and then creates an instance of the class. The inheriting class must override the run () method, which is the entry point for the new thread. It must also call the start () method to execute.
Three, the difference between thread and runnable
If a class inherits the thread, it is not suitable for resource sharing. However, if the Runable interface is implemented, it is easy to realize the resource sharing.
Summarize:
The benefits of implementing the Runnable interface are more than inheriting the thread class:
1): Suitable for multiple threads of the same program code to process the same resource
2): Can avoid the restriction of single inheritance in Java
3): Increase the robustness of the program, the code can be shared by multiple threads, code and data Independent
4): The thread pool can only be put into a class that implements runable or callable classes and cannot be placed directly into the inherited thread
Four. Conversion of thread state
Create: A new Thread object was created;
Ready state (Runnable): After the thread object is created, other threads call the object's start () method. The thread in that state is in a pool of running threads, becoming operational and waiting for the CPU to be used.
Running state (Running): The ready state of the thread gets the CPU and executes the program code.
Blocking state (Blocked): The blocking state is a temporary stop when the thread abandons the CPU usage for some reason. Until the thread is in a ready state, the opportunity to go to the running state is reached. There are three types of blocking:
wait for blocking: The running thread executes the wait () method, and the JVM puts the thread into the waiting pool. (wait will release the held lock)Synchronous blocking: When a running thread acquires a synchronization lock on an object, the JVM puts the thread into the lock pool if the synchronization lock is occupied by another thread. Other blocking: The running thread executes the sleep () or join () method, or when an I/O request is made, the JVM will place the thread in a blocked state. When the sleep () state times out, join () waits for the thread to terminate or time out, or the I/O process finishes, the thread is re-entered in a ready state. (Note that sleep does not release held locks)dead State (Dead): The thread finishes executing or exits the run () method because of an exception, and the thread ends the life cycle. Five. Common methodssleep (Long Millis): Allow the currently executing thread to hibernate (suspend execution) within the specified number of milliseconds join (): Refers to waiting for the T thread to terminate. yield (): Pauses the currently executing thread object and executes other threads, in most cases yield () will cause the thread to go from the running state to the operational state, but it may not have an effectthe difference between sleep () and yield (): sleep () causes the current thread to stall, so the thread executing sleep () is definitely not executed for the specified time, and yield () simply returns the current thread back to the executable state, so yield () is executed Threads are likely to be executed immediately after they have entered the executable state. The Sleep () method (Hibernate) is a static method of the thread class (threads), which causes the current thread to pause execution for a specified time, giving the execution opportunity (CPU) to another thread, but the lock on the object remains, so that it is automatically resumed after the sleep time has ended. Wait () is a method of the object class, and the Wait () method of the calling objects causes the current thread to discard the lock on the object (the thread pauses execution), into the object's wait pool (wait pools), only the Notify () method of the calling object (or Notifyall () method) to wake the thread in the wait pool into the lock pool, and if the thread re-obtains the lock on the object, it can enter the ready state. sleep () when sleeping, keep the object lock, still occupy the lock;
When Wait () sleeps, the object lock is released. Notify (): Notifies a thread to continue running and to obtain a lock token. Six. Thread Safety Generally understood as thread synchronization, the purpose of thread synchronization is to protect multiple threads to ask a resource when the destruction of resources, thread synchronization method is implemented by the lock, each object has a cut only a lock, the lock with a specific object, the thread once acquired the object lock, Other threads accessing the object no longer have access to other non-synchronous methods of the object, and when multiple threads wait for an object lock, the thread that does not acquire the lock will block, and the deadlock is the result of waiting for the lock between the threads, and the probability of the actual occurrence is very small. Really let you write a deadlock program, not necessarily good, hehe. However, once the program has a deadlock, the program will die.
the usual way to keep threads synchronized is to lock them, such as the Synchronized keyword. The Synchronized keyword can be used as a modifier of a function, or as a statement within a function, that is, a synchronous method and a synchronous statement block. In the case of finer classifications, synchronized can be used for instance variables, object reference, static functions, and class literals (literal constants of classes). As long as one thread accesses one of the synchronized methods, the other thread cannot access any of the synchronized methods in the object at the same time.
About Java Multithreading Preliminary