High concurrent programming basics

4) the difference between threads and processes :( compulsory)

answer:

  1. Process is an "execution program", it is an independent unit of system resource allocation and scheduling;
  2. A thread is a physical process, the process has a plurality of threads, a shared address space and other resources between threads (communication and synchronization, it is easier than the process of the thread);
  3. Thread context switches than the process context switch much faster.
    • (1) process switch, save it comes to the current process of the environment and the new CPU is scheduled to set CPU environments running processes.

    • (2) the thread switch requires only a small amount of saved register contents and settings, the operation does not involve the management of storage.

Interviewer: How interprocess communication? How inter-thread communication?

A: The inter-process communication IPC resources to rely on, such as pipes (pipes), sockets (sockets) and so on;

Communications between threads API JVM relies provided, for example, wait (), notify (), notifyAll () method and the like, between threads can also be passed by value shared main memory.

 

What is blocked (Blocking) and non-blocking (Non-Blocking)?

A: blocking and non-blocking usually used to describe the interaction between multithreaded. For example, a thread take up critical resources area, we need this and all other threads must wait resources in this critical area. Waiting causes the thread to hang, this situation is blocked. At this point, if the resource-thread has been reluctant to release the resources, all the other blocks on this critical area of ​​the threads are not working.

Non-blocking means contrary, it stresses that there is a thread can block other threads of execution. All threads will try to perform ever before.

 

What are the critical areas are?

A: The critical zone is used to indicate a public resource or shared resources that can be used by multiple threads. But each time, only one thread to use it, once the critical section resources are occupied, other threads To use this resource, you have to wait.

 

What is a deadlock (Deadlock), hunger (Starvation) and livelock (Livelock

Deadlock should be the worst case, and it represents more than two or processes in the implementation process, due to the competition for resources or A blocking phenomenon caused due communicate with each other, without external force, they are We will not be able to promote it. At this time, say the system is in deadlock state or system to produce a deadlock, which is always in the process of waiting for another process called the deadlock.

 

Hunger refers to one or more threads for various reasons unable to obtain needed resources, it has been unable to lead the implementation. For example:
1) its thread priority level may be too low, and the high priority thread to seize the resources it needs to continue, leading to a low priority thread does not work. In nature, when hens were fed chicks, it is prone to this situation, because a lot of chicks, limited food, food competition between chicks can be very powerful, because the chicks are often not grab foods, there may be starve to death. Thread starvation is very similar to this case.
2) Another possibility is that a thread has been occupied and hold the key resources, causing other threads need this resource can not be executed properly, one such case is hunger.
Compared with the deadlock, hunger is still possible to resolve in the next period of time (such as high-priority thread has completed the task, no longer perform crazy)

 

Livelock is a very interesting situation. I do not know there is not encountered such a situation, when you want to take the elevator downstairs to the elevator door opened, then you are ready to go out, but unfortunately, a man blocked the door of your path, he wanted to come. So you are a gentleman left to go, avoid each other, but at the same time the other side is also very gentleman, but he clued you want right away. As a result, you also hit. Ever since, you are aware of the problem, hoping to avoid each other as soon as possible, you go immediately to the right, and he immediately left to go, the result has hit! However, only between humans, I believe that after this action was repeated three times, you should be able to successfully solve this problem, because this time, we instinctively on the TV, to communicate, to ensure that this situation does not recur.
But if this happens between two threads may not be so lucky, if the thread of intelligence is not enough, and are adhering to the principle of "humility", the initiative will free up resources for others to use, then there will be resources to continue in two threads in the beating, but no one thread can get all the resources while the normal execution. This situation is livelock.

 

How to avoid deadlocks? (Frequently ask this question then oh ~)

A: acquire the lock specified sequence, for example as follows:

  1. Only with such a thread lock A and B in order to lock a resource to operate under conditions in multithreaded

  2. Is a certain order to acquire a lock, such as provisions only get A lock thread qualify for the lock B to acquire a lock in order to avoid deadlock! ! !

  1. New (NEW) state: that creates a new thread object, but this time thread does not begin execution.

  2. Run (RUNNABLE) state: The thread object is created, other threads (such as main thread) calls the object's start () method, it indicates that the thread begins execution. When the thread execution, in RUNNBALE state, represents all the resources needed to thread ready. The thread state is located runnable threads in the pool, waiting to be selected thread scheduling, acquiring the right to use the cpu.

  3. Blocked (BLOCKED) states: If the thread during execution and finally to the synchronized sync blocks, will enter BLOCKED blocked, then the thread will be suspended until the lock is obtained request.

  4. Waiting (the WAITING) Status: waiting for another thread when a thread scheduler notifies a condition enters a wait state of its own. When you call Object.wait Thread.join method or methods, or wait for the library java.util.concurrent Lock or Condition, it will happen;

  5. Timing wait (TIMED_WAITING) status: Object.wait, Thread.join, Lock.tryLock and Condition.await methods timeout parameters, as well as Thread.sleep method, LockSupport.parkNanos method and LockSupport.parkUntil methods, which causes the thread timing enter the wait state, a timeout or if notification occurs, it switches will be operational state;

  6. Termination (TERMINATED) state: When the thread is finished, then enter the state, indicating the end.

Note: After starting from the state NEW, NEW thread can not go back to the state the same token, the thread is TERMINATED state can no longer return to RUNNABLE state.

 

  1. sleep method: a static method of the Thread class, the current thread will sleep n milliseconds, the thread into the blocked state. When the sleep time is up, unblocked, be runnable, waiting for the arrival of the CPU. Sleep does not release the lock (if any);

  2. wait method: a method of Object, must be used with the synchronized keyword, thread into the blocked state, when the notify or notifyall is called, will be unblocked. However, only after re-occupy the mutex will be runnable. Sleep, release the mutex.

2) synchronized keyword:

A: The underlying implementation:

  1. Entering, performing the monitorenter, counter + 1, the lock is released when the monitorexit, the counter 1;

  2. When a thread is determined when the counter is 0, the current lock is free to be occupied; on the contrary, the current thread enters a wait state.

Meaning: (monitor mechanism)

Synchronized is locked, plus the object lock. The weight of the object lock is a lock (monitor), synchronized locking mechanism would have biased locking (single thread) at run time based on the thread competition, lightweight lock (multiple threads to access synchronized region), the object lock (lock weight, the presence of competing multiple threads), the spin lock.

 

4) volatile enables a non-atomic operation into atomic operation it?

A: Yes.

A typical example is a long member variable in the class. If you know the member variable is accessed by multiple threads, such as counters, and prices, you'd better set it as volatile. why? Since reading a long type variable in Java atoms not need to be divided into two steps, if a long thread is a modified value of the variable, another thread may see only half (the first 32 bits) of the value. However, the reading and writing to a volatile variable is long or double atom.

 

the volatile modifier had any practice?

answer:

  1. A practice is that volatile long and double variable, so that it can be read by atom type. double and long are 64 bits wide, so the two types of reading is divided into two parts, the first reading of a 32-bit, 32-bit and then read the rest of the process is not atoms, However, Java, or read-write volatile type long double variables are atomic.

  2. Another function is to supply the repair volatile memory barrier (memory barrier), for example, in a distributed application framework. Simply put, that is, when before you write a volatile variable, Java memory model inserts a write barrier (write barrier), before reading a volatile variable, inserts a read barrier (read barrier). His point is that when you write a volatile region, to ensure that any thread can see the value of what you write, while, before writing, but also guaranteed to be visible to update any values ​​for all threads, because the memory barrier will All other values ​​written update to the cache.

Guess you like

Origin www.cnblogs.com/zj-xu/p/11778011.html