I. Description
Weekend time to re-learn the next multi-threading, in order to facilitate later review, write down the study notes.
The key to effective use of multithreading is to understand that programs are executed concurrently rather than serially. For example, there are two subsystems in the program that need to be executed concurrently, which requires multithreaded programming.
With the use of multithreading, you can write very efficient programs. However, if too many threads are created, the efficiency of the program execution is reduced.
At the same time the switching overhead of the context is also important, if too many threads are created, the CPU spends the switching time in the context for the time the program executes.
Second, Java multithreaded programming
In learning Multi-threading, we should first understand another concept.
Process: is a computer program on a data set on a running activity, the system is the basic unit of resource allocation and scheduling, is the basis of the operating system structure. In contemporary thread-oriented computer architecture, a process is a container for threads. A program is a description of instructions, data, and its organization, and the process is the entity of the program. A process includes the memory space allocated by the operating system, which contains one or more threads. A thread cannot exist independently, it must be part of a process. A process runs until all non-waiting threads end up running.
- The life cycle of a thread
A thread is a process that executes dynamically, and it also has a process from generation to death. the life cycle of the thread is displayed:
Description
New status:
When a thread object is established using the New keyword and the thread class or its subclasses, the thread object is in the new state. It keeps this state until the program start () this thread.
Ready State :
When the thread object calls the start () method, the thread enters the ready state. The ready state thread is in the ready queue, waiting for the thread scheduler in the JVM to dispatch.
Running State :
If the ready state thread gets the CPU resources, it can execute run (), at which point the thread is in the running state. A running thread is the most complex and can become a blocking state, a ready state, and a dead state.
Blocking State :
If a thread executes a method such as sleep, suspend (hangs), and so on, the thread will enter a blocking state from the running state after the resource is lost. You can re-enter the ready state after the sleep time has arrived or the device resource has been acquired. Can be divided into three kinds:
Wait for blocking: the thread in the running state executes the wait () method, which causes the thread to enter a wait-blocking state.
Synchronous blocking: The thread acquires a synchronized synchronization lock failure (because the synchronization lock is occupied by another thread).
Other blocking: When an I/O request is made by the calling thread's sleep () or join (), the thread enters the blocking state. When the sleep () state times out, the join () waits for the thread to terminate or time out, or the I/O process completes and the thread is re-entered in a ready state.
Death Status :
When a running state thread finishes a task or other termination condition occurs, the thread switches to the terminating state.
Each Java thread has a priority, which helps the operating system determine the scheduling of threads.
The priority of a Java thread is an integer whose value range is 1 (thread.min_priority)-Ten (thread.max_priority). By default, each thread will be assigned a priority of norm_priority (5).
A thread with a higher priority is more important to the program, and the processor resources should be allocated before a low-priority thread. However, thread precedence does not guarantee the order in which threads are executed, and is very dependent on the platform.
Java provides three ways to create threads:
By implementing the Runnable interface;
By inheriting the thread class itself;
Creating threads through callable and future;
1. Create a thread by implementing the Runnable interface
The simplest way to create a thread is to create a class that implements the Runnable interface.
To implement Runnable, a class simply executes a method call to run (), declared as follows:
public void run ();
After you have created a class that implements the Runnable interface, you can instantiate a thread object in the class.
Thread (Runnable threadob,string threadname);
Threadob is an instance of a class that implements the Runnable interface, and ThreadName specifies the name of the new thread.
After the new thread is created, calling the start () method will run.
Void start ();
Examples are as follows:
2. Create threads by inheriting thread
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.
Although this method is listed as a multi-threaded implementation, it is also an example of implementing the Runnable interface in essence.
Examples are as follows:
3. Create threads through callable and future
The steps are as follows:
Create an implementation class for the callable interface and implement the call () method, which will act as the thread execution body and have a return value.
Creates an instance of the callable implementation class, using the Futuretask class to wrap the callable object, which encapsulates the return value of the call () method of the Callable object.
Creates and starts a new thread using the Futuretask object as the target of the thread object.
Call the Get () method of the Futuretask object to get the return value after the child thread execution ends.
Description: A comparison of three ways to create threads
When multiple threads are created in a way that implements Runnable, callable interfaces, the thread class simply implements the Runnable interface or callable interface and can inherit other classes.
When creating a multi-line city with the inheritance of the thread class, it is easy to write, and if you need access to the current thread, you do not need to use the Thread.CurrentThread () method to get the current thread directly using this.
Note: common thread () methods
public void Start (): Causes the thread to start executing, and the Java Virtual machine calls the thread's Run method.
public void Run (): If the thread is constructed using a standalone runnable run object, the Run method of the Runnable object is called, otherwise the method returns without performing any action.
Public final void SetName (String name): Change the name of the thread.
Public final void SetPriority (Int. priority) : Changing the priority of a thread
Public final void Setdaemon (Boolean on): marks the thread as either a daemon thread or a user thread.
Public final void Join (Long millisec): The maximum time to wait for the thread to terminate is milliseconds.
Public void Interruput (): the thread is disconnected.
Public final Boolean isAlive (): Tests whether the thread is active.
public static void Yield (): Pauses the currently executing thread object and executes other threads.
public static void sleep (Long millisec): Allows the executing thread to hibernate within the specified number of milliseconds.
public static Boolean Holdslock (Object x): Returns True if and only if the monitor lock is persisted on the specified object by the front thread.
public static Thread CurrentThread (): Returns a reference to the currently executing thread object.
public static void DumpStack (): Prints the stack trace of the current thread to the standard error stream.
Resources:
Http://www.runoob.com/java/java-multithreading.html
Java multithreaded Programming (learning notes)