First, multi-threaded basis

Source: Internet
Author: User

1. Synchronous and asynchronous
For example, the implementation of two methods, synchronization means that the 1th method executes the return result and then continue to execute the 2nd method, while asynchronous means that the method execution does not equal its result, another method executes sequentially, so the result returns a delay (Ajax call)
Synchronization characteristics: To ensure the orderly implementation of the method, the shortcomings of slow response
Asynchronous features: Fast response, but can not guarantee an orderly execution of results, such as Method 1 execution, the result has not returned, Method 2 has been executed, if this is required to use the results of Method 1 as a parameter to execute Method 2, then the program will be error
2, concurrent (single CPU) and parallel (multiple CPUs) but because the CPU execution speed, seemingly all at the same time, so generally do not make a distinction
3, the critical area multi-threaded common access to shared resources, there is thread safety, so this area needs synchronization control
4. Blocking (blocking) thread hangs
Non-blocking (non-blocking) allows multiple threads to enter a critical section
5. Deadlock (Deallock)
/**
* Dead Lock Example
* 1, two threads t1,t2 two resources s1,s2
* 2, T1 execute S1 resource (locked), T1 sleep 2 seconds, lock not released
* 3, T2 execute S2 resource (locked), continue to request S1 resource (locked)
* 4, T1 after 2 seconds to continue execution, request S2 resources (locked)
* 5, Result: T1 request S2 resource Discovery S2 resource locked into wait state
* T2 request S1 Resource Discovery S1 resource locked into wait state
* T1 and T2 are unable to continue execution, enter the program unresponsive status
* Avoid deadlocks: One thread avoids multiple locks, avoids a thread that takes up multiple resources within the lock, attempts to use timed lock Lock.trylock (timeout), locks and unlocks in a database connection for database locks, or fails to unlock
*/

public class _1deadlock {
private static String s1= "Resource 1";
private static String s2= "Resource 2";

public static void Main (string[] args) {
New _1deadlock (). DeadLock ();
}
public void DeadLock () {
Thread 1
Thread T1=new Thread (new Runnable () {
@Override
public void Run () {
Synchronized (S1) {//Lock resource 1
try {
Thread.Sleep (2000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
Synchronized (S2) {
System.out.println ("1");
}
}
}
});
Thread 2
Thread T2=new Thread (new Runnable () {
@Override
public void Run () {
Synchronized (S2) {
Synchronized (S1) {
System.out.println ("2");
}
}
}
});
T1.start ();
T2.start ();
}
}

Starvation deadlock: In a thread pool, if a thread waits for another thread to end to execute (in other words, depends on another thread), but if this thread does not end properly for a special reason, the waiting thread is starved and never executes
Live Lock (Livelock) elevator met people, the door opened, met people, inside and outside people want to avoid each other, but inside people left, outside people are also left, inside and outside the people are performing the same direction of the evasion, the results of the road has been blocked
6, concurrency Level: blocking 4, barrier free 3, no lock 2, no wait 1 (all read threads)
7. Parallel law
Amdahl (Amdahl Law) tells us that the number of CPUs is not the more the better, because according to this law, when the number of CPUs, and the steps of the walk is constant, then the speedup will be less and less
For example, the execution of a program, requires 5 steps to complete, 2nd and 5th steps to 2 CPU parallel computing, the speedup than =1/(2/5*1/2+3/5) =1.25
Gustafson (Gustafson Law) tells us that as long as there is enough parallelism, the speedup is proportional to the number of CPUs
Execution Time A+b (a indicates that serial time B represents parallel time) total execution time A+NB (n is the number of processors) acceleration ratio s (n) =a+nb/(A+B) due to serial proportional f=a/(A+B) to deduce the result of the Speedup (n) =n-f (n-1)
8. What is thread thread is an execution unit within a process
9. Thread Operation:
New (Create thread)-----> start (starting thread)----->runnable (Execution specific method) terminated (Close thread)
Synchronized (synchronous)---->blocked (blocking thread) wait (wait)---->waiting (Waiting)---->notify (Wake-up) timed_waiting (wait for a limited time)
10. Thread yield and join
Thread.yield () is a static method that threads concessions, giving other threads a chance to compete for CPU
T1.join (); After the join method is executed, the other threads enter the wait state, and when T1 executes, Notifyall () is called in the JVM to wake up the other threads. For example, you need to T1 execution (such as data saving), and then execute the following code
11, the termination of the thread
Thread.stop () is not recommended, too violent, will release all locks, causing the phantom reading of the data, such as T1 is writing data, write half, stop, will release the lock, and this is T2 will read to modify half of the data, resulting in data read error
Thread.Interrupt () thread interrupt
12. Sleep hibernation throws an interrupt exception, which empties the interrupt flag, so continuing the interrupt operation in the exception will be effective
13, suspend (hang) resume (continue) These two methods are not recommended, the reason is that the retention is also compatible with the old version, because suspend will not release the lock, multi-threaded resume may occur before the suspend, may cause the thread freezes state, permanent hangs,
If a thread may be waiting for the thread to release the lock, it can cause a deadlock
JPS View the Java process and then jstack the PID Dump
14, daemon thread (background thread) Setdaemon (true), such as garbage collection, etc., when the main thread ends, the daemon thread exits naturally
15, Thread Priority t1.setpriority (10); The higher the priority, the CPU first executes the thread
16. Synchronization of Threads synchronized
17, object.wait (); Object.notify (); Note that using wait must first obtain the lock on the monitor object, which means that the thread must be placed in the synchronized synchronization method block

First, multi-threaded basis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.