java multithreading: Thread base

1. The difference between threads and processes

Each program running on the system is a process. Each process contains one or more threads. A thread is a set of a set of instructions, or the special section of the program, which can be independently executed in the program. It can also be understood as context code run. So basically a lightweight process thread, which is responsible for performing multiple tasks in a single program. Usually responsible for scheduling and execution of multiple threads by the operating system.

Using threads can occupy a long time in the program into the background task to deal with, running speed of the program is likely to accelerate in the realization of some tasks, such as waiting for user input, file read and write, and send and receive network data, the thread is more useful . In this case you can free up some valuable resources such as memory usage and so on.

If a large number of threads , will affect the performance, because the operating system needs to switch between them, more threads require more memory space, thread suspension need to consider their impact on the program running. Typically model data block is shared among a plurality of threads, the thread needs to prevent a deadlock situation.

Summary : The process is the set of all threads, each thread is a path of execution process.

Process is a stand-alone application such as qq, wx

Thread execution code is the smallest unit, an execution path, such as a plurality of Thunder downloaded movies, multi-threaded download, database connection pooling, a batch send text messages, thread pool

 

Multithreading not able to open too much, consume too much cpu, multi-threaded purpose is to improve the efficiency of code execution

II. Multithreading

1. What threads are? A thread is a path of execution, each thread independently of each other

2. What is multithreading? Multithreading is in the same process, a number of different execution paths, in parallel, in order to improve program efficiency

3. there must be a process in the main thread

4. Thread Category:

Java, there are two threads, one kind is the user thread, the other is the guardian of the thread.

User thread is refers to the user to customize threads created , the main thread is stopped, the user thread will not stop

Daemon thread when the process does not exist or the main thread to stop , daemon thread also will be stopped .

Use setDaemon (true) method to three daemon thread, synchronous and asynchronous

1. The concept of synchronization, code is executed from the top down, the synchronization request HTTP

2. Asynchronous concept, in parallel, independently of each other, such as ajax

Fourth, multi-threaded approach

1. inherited Thread, override the run method

2. To achieve Runnable, implement the run method

3. anonymous inner classes, direct new Runnable () {run {}}

3. Thread Pool

Fifth, the state of the thread

 Thread from creation, always run to the end in one of the following five states: New state, ready state, running state, blocking state and the state of death.

New build state

   When a new Create a thread, the operator, for example, new the Thread (R & lt) , the thread has not started to run, in this case a new thread state. When a thread is in the nascent state, the program code that has not yet started running thread

Ready status

A new thread is created does not start automatically, to execute the thread, the thread must call the start () method. When calling thread object start () method to start a thread that is, start () to create a system resource thread running approach and schedule threads run run () method. When the start () method returns, the thread is in the ready state.

     Thread a state of readiness does not necessarily run immediately run () method, the thread must also compete with other thread CPU time, only to get CPU time before they can run threads. Because a single CPU computer system, it is impossible to run multiple threads simultaneously, one time only one thread is running. Therefore, at this time there may be multiple threads in the ready state. Multiple threads in the ready state is determined by the Java runtime system thread scheduler ( the Thread Scheduler ) to schedule.

Operating status

When the thread gets CPU time after it went into operation before the real start execution run () method .

Blocking state

    Thread running process, it may be due to various reasons into the blocked state :       

  1> thread by calling sleep sleep method;       

  2> thread calls in a I / O to be operatively blocked, i.e., the operation does not return to its caller before the input-output operation is completed;       

  3> Thread trying to get a lock, but the lock is being held by another thread;         

  4> thread is waiting for a trigger condition;

Death state

There are two reasons why the thread death:   1) run method exits normally and died of natural causes,    2) an uncaught exception terminates the run method of the thread sudden death.   In order to determine whether the current thread alive (that is, either can be run either be blocked), use isAlive method. If you are running or blocked, this method returns to true ; if the thread is still new state and can not run, or the thread dies, it returns false.

join () method acting

If the thread t1 and t2 thread is a user thread, let t1 t2 executed after their execution: Join t2.join in the run method of t1, I want to join A to B in front of it in A Method B .join

 

When the main thread to execute them when t1.join () method, it is considered the main thread execution should be ceded t1

 

Requirements: create a thread, the child thread is finished, the main thread to perform.

 

Thread t1 = new Thread(new Runnable() {

 

@Override

public void run() {

for (int i = 0; i < 10; i++) {

try {

Thread.sleep(10);

} catch (Exception e) {

 

}

System.out.println(Thread.currentThread().getName() + "i:" + i);

}

}

});

t1.start();

// When the main thread to execute them t1.join () when the method is considered the main thread should execute ceded t1

t1.join();

for (int i = 0; i < 10; i++) {

try {

Thread.sleep(10);

} catch (Exception e) {

 

}

System.out.println("main" + "i:" + i);

}

 

VI. Daemon thread

 Java, there are two threads, one kind is the user thread, the other is the guardian of the thread.

 User thread is refers to the user to customize threads created , the main thread is stopped, the user thread will not stop

Daemon thread when the process does not exist or the main thread to stop , daemon thread also will be stopped .

 Use setDaemon (true) method to set a daemon thread

Non-daemon thread features: the main thread and affect each other

It concluded: the user creates a thread, even when the main thread ends, is not the end,

When the user threads to daemon thread, use: t1.setDaemon ( to true); end of the main thread, he also ended

 

Guess you like

Origin www.cnblogs.com/hejunhong/p/11415857.html