A simple definition of multi-threaded!

When it comes to thread 1, first of all for the next process, the following is the definition of the process:

Process structure is the underlying operating system, is a program execution, is active that occurs when a program and data structure sequentially executed on the processing engine, a process running on a data set, which is a resource allocation system, and a separate unit of scheduling.

In simple terms an exe file a Task Manager list can be understood as a process, such as QQ.exe is a process, the process is the basic unit run by the system administration.

1.1 What is a thread?

A thread is the smallest unit of an operating system capable of operation schedule, which is included in the process, the actual operation of the unit process.

In simple terms, a thread can be understood as a separate sub-tasks running in the process. For example, QQ.exe running there are a lot of sub-tasks running simultaneously.

1.2 The difference between processes and threads

1.2.1 Scheduling: as the basic unit of thread scheduling and allocation process as the basic unit of own resources.

1.2.2 Concurrency: not only between processes can execute concurrently across multiple threads in the same process can also be executed concurrently.

1.2.3 have the resources: the process is an independent unit with the resources, the threads themselves have essentially no system resources, has only a little in the operation of essential resources (such as a program counter, a set of registers and stack), but it can share the process with other threads belong to a process that is owned by all of the resources. Between processes is not shared address space, and the thread is a shared address space where the process is.

1.2.4 overhead: When you create or undo the process, because the system must be recomputed and resource recovery, leading to system overhead is significantly greater than the cost of creating or revoked when the thread.

1.3 What is a multi-threaded?

A simple definition of multi-threading

 

Multithreading is virtually execute multiple threads simultaneously.

1.4 Why use multithreading

1.4.1 using threads can occupy a long time in the program into the background task to deal with.

1.4.2 user interface more attractive, so for example the user clicks a button to trigger the processing of something pieces, you can pop a progress bar to show the progress of the process.

1.4.3 operating efficiency of the program could be improved.

1.4.4 On some tasks, such as waiting for user input to achieve, file read and send and receive network data, the thread is more useful.

State 2 thread

In general, these threads include the following states: create (new), Ready (runnable), running (running), obstruction (blocked), timed_waiting, waiting, dying (dead).

A simple definition of multi-threading

 

More than three-threaded use

3.1 Thread class inheritance

A simple definition of multi-threading

 

The program can be seen in alternately performed, but eventually implemented to 98.

3.2 implement Runnable

A simple definition of multi-threading

 

You can still be seen in the alternate program execution, but eventually implemented to 98.

3.3 inherit the implementation of Callable Interface

A simple definition of multi-threading

 

It should be noted that to achieve Callable interface, you must rewrite the call () method, and to use FutureTask class, will not be discussed here first, and so on and then go into detail later update thread pool.

For example, by using a thread pool 3.4 Executor framework

This section describes temporarily. Back and update the thread pool Executor will be described in detail.

4 Use the multi-threaded certain fast?

A: Not necessarily, because the multi-thread context switch overhead context switch will bring.

4.1 What is a context switch?

For single-core CPU, CPU can only run one at a time threads, when transit during operation of a thread to run another thread, this is called a thread context switch (for the process is similar). Thread context switching process will record the program counter, state of the CPU registers and other data.

4.2 How to reduce context switching?

4.2.1 reduce the number of threads

Because each time a CPU can execute only one thread, playfully we want the program to be executed concurrently, the operating system context switches had constantly to make us feel from the sensory program is concurrent execution of the line. Therefore, as long as we reduce the number of threads, it can reduce the number of context switches.

However, if the number of threads have been less than the number of CPU cores per CPU executes a thread would be reasonable CPU context switching is not required, but it is not.

4.2.2 control the same number of locked threads

If multiple threads share the same lock, then when a thread to acquire the lock, other threads will be blocked; When this thread releases the lock, the operating system performs a selected thread is blocked, so that a context switch will appear.

Thus, reducing the number of threads of the same lock can also reduce the number of context switches.

4.2.3 The use of lock-free concurrent programming

Concurrent execution of tasks need to be stateless: HASH segments

所谓无状态是指并发执行的任务没有共享变量,他们都独立执行。对于这种类型的任务可以按照ID进行HASH分段,每段用一条线程去执行。

需要并发执行的任务是有状态的:CAS算法

如果任务需要修改共享变量,那么必须要控制线程的执行顺序,否则会出现安全性问题。你可以给任务加锁,保证任务的原子性与可见性,但这会引起阻塞,从而发生上下文切换;为了避免上下文切换,你可以使用CAS算法, 仅在线程内部需要更新共享变量时使用CAS算法来更新,这种方式不会阻塞线程,并保证更新过程的安全性。

5 使用多线程的缺点:

5.1 上下文切换的开销

当 CPU 从执行一个线程切换到执行另外一个线程的时候,它需要先存储当前线程的本地的数据,程序指针等,然后载入另一个线程的本地数据,程序指针等,最后才开始执行。这种切换称为“上下文切换”。CPU 会在一个上下文中执行一个线程,然后切换到另外一个上下文中执行另外一个线程。上下文切换并不廉价。如果没有必要,应该减少上下文切换的发生。

5.2 增加资源消耗

线程在运行的时候需要从计算机里面得到一些资源。 除了 CPU,线程还需要一些 内存来维持它本地的堆栈。它也需要 占用操作系统中一些资源来管理线程。

5.3 编程更复杂

在多线程访问共享数据的时候,要考虑 线程安全问题 。6 线程安全

6.1 线程安全定义

一个类在可以被多个线程安全调用时就是线程安全的。

6.2 线程安全分类

线程安全不是一个非真即假的命题,可以将共享数据按照安全程度的强弱顺序分成以下五类:

不可变、绝对线程安全、相对线程安全、线程兼容和线程对立。

6.2.1. 不可变

不可变(Immutable)的对象一定是线程安全的,无论是对象的方法实现还是方法的调用者,都不需要再采取任何的线程安全保障措施,只要一个不可变的对象被正确地构建出来,那其外部的可见状态永远也不会改变,永远也不会看到它在多个线程之中处于不一致的状态。

Type immutable: final keyword modified basic data types; String; enum type; Number The portion subclasses such as Double values ​​and other types of packaging Long, a BigInteger BigDecimal and other large data type. However, the same type Number of sub-atomic and AtomicInteger AtomicLong is not immutable.

6.2.2 Absolute thread safety

Regardless of the runtime environment, the caller does not need any additional synchronization measures.

6.2.3 Relative thread-safe

Relative thread-safe operations on the need to ensure that separate this object is thread safe, when calling does not need to do additional safeguards, but for continuous call some particular order, you may need to use additional means to synchronize the call to end to ensure the correctness of the call.

6.2.4 compatible thread

Thread-compatible means that the object itself is not thread-safe, but may end by calling the proper use of means to ensure synchronization objects can be safely used in a concurrent environment, we usually say that a class is not thread-safe, most of the time means this is a situation. Most of the Java API classes are of thread-compatible, such as the previous corresponding Vector and HashTable collection classes ArrayList HashMap and the like.

6.2.5 thread-hostile

Thread-hostile refers to the code that calls the end regardless of whether synchronization has taken measures can not be used concurrently in a multithreaded environment. Since the Java language born with characteristics of multi-threading, thread-hostile rejection of this multi-threaded code is rare, and often are harmful and should be avoided.

Finally, Xiao Bian finishing a bit on distributed micro-services, performance optimization, recording video and data Spring, and other source of knowledge of MyBatis! And a variety of interview questions and information Oh!

 

Please add skirt 772,300,343 need, you can receive!

 

Guess you like

Origin www.cnblogs.com/sevencutekk/p/11511701.html
Recommended