java high concurrency ---- personal study and understand the summary record

1. First, we need to understand a few concepts

1. Synchronous (Synchronous): Once you start the synchronization method call, the caller must wait until the call to return to the previous method, in order to continue the follow-up actions in turn until all.

2. asynchronous (Asynchronous): asynchronous method invocation a messaging like, once started, the method call returns immediately, the caller can continue the subsequent operations. Asynchronous method typically performs "real" in another thread. The whole process, not hinder the work of the current caller.

3. Concurrent (Concurrency): in a period of time, more than one thing alternately performed during this time. For example: There are two queues, into the same inlet, alternately enter.

4. Parallel (Parallelism): multiple things happen at the same time colleagues. For example: There are two queues, respectively, into two such inlets. Without disturbing each other.

Note: In fact, if there is only one CPU in the system, the use of multi-process or multi-threaded task, then the real environment, these tasks can not be true parallel, after all, a CPU can execute only one instruction, and more in this case or multi-threaded process is concurrent, not parallel (operating system will continue to multi-task switching). True only occur in parallel system has a plurality of CPU's (such as a multi-core CPU).

5. critical region: critical section is used to indicate a public resource or shared data can be used by multiple threads, but each time only one thread to use it, once the critical section resources are occupied, in order to use this other thread resources will have to wait.

6. blocked (Blocking): taking up a thread critical section resources, other threads must wait in this critical area, causes the thread to hang, this situation is blocked.

7. Non-blocking (Non-Blocking): A thread can not interfere with other threads, all threads will attempt to continue to perform forward.

8. deadlock (Deadlock): Several threads are not releasing resources, lack of resources they need, resulting in no way threads continue.

Note: can be viewed by jstack command where pid needed to get through the command line, it can also be provided by the JDK jconsole.exe directly view the current thread pid tool. Do not understand their own Baidu

9. Hunger (Starvation): refers to one or more threads for various reasons can not get the resources you want, lead has been unable to perform. For example, it may be too low a priority, and high priority thread continues to seize the resources it needs, resulting in a low-priority thread does not work

10. Livelock (Livelock): for example, two threads are adhering to the principle of "humility", the initiative will free up resources for others to use, may lead to resources constantly beating between the two threads, one thread can get without at the same time All resources normally. This situation is livelocks

Summary: The concept here is to learn together more boring, more than were understood Practice makes perfect.

2. The concept of concurrency level

According to concurrent control strategy, we can put into the level of concurrent obstruction , no hunger , barrier-free , lock-free , no-wait several.

1. obstruction: a thread is blocked, then release resources before other threads, the current thread can not proceed. When we use the synchronized keyword or reentrant lock , we get is blocked thread.

2. No Hunger: between threads is a priority, then thread scheduling time always tend to meet the high priority thread. Which is divided into non-arm lock and fair locks in both cases. Fair lock regardless of the new thread priority to high, in order to obtain resources, we must obediently line up, so that all threads have the opportunity to perform

3. Roll: Roll is a non-blocking scheduling weakest. If two threads unimpeded execution, because the problem will not lead to a critical section of the party is suspended. Everyone modify shared data, the data mess out how to do it? For accessibility thread, once this situation is detected, it will immediately roll back the modifications that he has done, to ensure data security.

4. No Lock: no lock are accessible in parallel. Under no lock case, all threads can attempt to access the critical section, but the difference is, lock-free concurrency to ensure that there must be a thread to complete the operation leaves the critical area within a finite number of steps.

5. Wait None: no lock requires only a thread operation can be completed in a finite number of steps, without waiting for further extended on the basis of the lock-on. It requires that all threads must be completed within a finite number of steps, this will not cause hunger. If the upper limit of this step can be further broken down into a bounded number of threads without waiting and waiting and so are several non-independent, the difference between them is only to limit the number of cycles of different

Two important laws of 3 parallel

Why use the previous problem of parallel programs have a simple discussion. Overall, the most important thing should be in two purposes.

First, in order to obtain better performance;

Second, due to business model does require multiple execution entities.

Here, more attention to the first case, the problem is related to performance. The serial program for the transformation of concurrent programs, in general, can improve the overall performance of the program, but how much can be improved, even to say exactly whether it can be improved, is still a problem to be studied. Currently, there are two laws to answer this question, one Amdahl's law , the other is Gustafson's law .

About these two laws, not quite understand, more in-depth customer research on their own Baidu, in general, ways to improve performance: to find ways to improve the proportion of parallel systems, while increasing the number of the CPU .

4.JMM (java memory model) related concepts

JMM key technical point revolves around multi-threaded atomicity, visibility, ordering to build

1.原子性:是指操作是不可分的,要么全部一起执行,要么不执行。在java中,其表现在对于共享变量的某些操作,是不可分的,必须连续的完成。比如a++,在操作a++的过程中,其他线程不会改变a的值。

注意:java中实现原子操作的方法大致有2种:锁机制、无锁CAS机制

2.可见性:可见性是指一个线程对共享变量的修改,对于另一个线程来说是否是可以看到的。比如:线程A对共享变量的修改要被线程B及时看到的话,需要进过以下步骤:1.线程A在自己的工作内存中修改变量之后,需要将变量的值刷新到主内存中 2.线程B要把主内存中变量的值更新到工作内存中

注意:可以使用volatilesynchronized来实现

3.有序性:有序性指的是程序按照代码的先后顺序执行。为了性能优化,编译器和处理器会进行指令冲排序,有时候会改变程序语句的先后顺序

5.理解进程和线程

1.进程:我们经常使用windows系统,经常会看见.exe后缀的文件,双击这个.exe文件的时候,这个文件中的指令就会被系统加载,那么我们就能得到一个关于这个.exe程序的进程。

2.线程:线程是轻量级的进程,是程序执行的最小单元,使用多线程而不是多进程去进行并发程序的设计,是因为线程间的切换和调度的成本远远小于进程,线程的所有状态在java.lang.Thread中的State枚举中有定义:

  New:表示刚刚创建的线程,这种线程还没有开始执行

  RUNNABLE:运行状态,线程的start()方法调用后,线程会处于这种状态

  BLOCKED:阻塞状态。当线程在执行的过程中遇到了synchronized同步块,但这个同步块被其他线程已获取还未释放时,当前线程将进入阻塞状态,会暂停执行,直到获取到锁。当线程获取到锁之后,又会进入到运行状态(RUNNABLE)

  WAITING等待状态。和TIMEWAITING都表示等待状态,区别是WAITING会进入一个无时间限制的等,而TIMEWAITING会进入一个有限的时间等待,那么等待的线程究竟在等什么呢?一般来说,WAITING的线程正式在等待一些特殊的事件,比如,通过wait()方法等待的线程在等待notify()方法,而通过join()方法等待的线程则会等待目标线程的终止。一旦等到期望的事件,线程就会再次进入RUNNABLE运行状态。

  TERMINATED表示结束状态,线程执行完毕之后进入结束状态。

注意:从NEW状态出发后,线程不能在回到NEW状态,同理,处理TERMINATED状态的线程也不能在回到RUNNABLE状态 

个人理解:单个CPU一次只能运行一个任务,任一时刻,CPU总是运行一个进程,其他进程处于非运行状态。其中,一个进程可以包括多个线程,一个进程的内存空间是共享的,每个线程都可以使用这些共享内存。需要注意的是:一个线程使用某些共享内存时,其他线程必须等它结束,才能使用这一块内存

注意:以上全部都是基本概念的理解,对于高并发的代码技巧掌握,不要急于求成,更多的是要着重于每一个基础知识点的逐步积累,万丈高楼平地起,一起加油骚年们。

 

 

 

Guess you like

Origin www.cnblogs.com/Roger2Lj/p/11562509.html