table of Contents
Concurrent debugging, tools, containers!
references:
Tomcat7 optimize performance, improve concurrency -NIO mode
Singleton multithreading issues thoroughly get to know the interview asked of spring
A detailed understanding of singleton and multiple threads + A + interview questions inside tips
volatile keyword
Volatile keyword effect (visibility of variables, reordering prohibited), can not be guaranteed atomicity
- scenes to be used
- Usually modified state (boolean and int)
- Simple assignment
Synchronization mechanism
Monitor lock (the synchronized)
Pessimistic locking
Display lock (ReentrantLock, ReadWriteLock)
Also provided in response to such an interrupt may be locked, the lock request may be a polling timing lock avoid deadlock Multithread
Atomic variables (AtomicInteger, AtomicLong, AtomicBoolean)
Optimistic locking, CAS (compare mention change)
volatile
Thread closed (analog single-threaded, multi-threaded environment aversion)
Do not share data
- Stack closed
- ThreadLocal
Starting gun
CountDownLatch (thread counter)
static CountDownLatch cdl = new CountDownLatch(20);
...
cdl.countDown(); // 此处要调用20次
...
cdl.await(); // 阻塞,countDown计数20次后才统一走下一步程序
// 类似发令枪,统一起跑
The default IO model and Tomcat8 the difference Tomcat7
BIO tomcat7 default, a thread corresponding to a request, blocked
tomcat8 the NIO default, a thread can handle multiple requests, nonblocking
Start NIO mode
Modify tomcat7 to NIO
<Connector port="8080"protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" redirectPort="8443"/>
Thread Pool
The tomcat is not enabled by default thread pool, in tomcat each user request is a thread, so you can use the thread pool to improve performance. In fact, there is a reception here thread scheduling and dispatching thread into the thread pool, to a certain time and then to the thread pool task into a worker thread
Release profile of the notes below, and specifies the actuators Connector
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> <!-- executor="tomcatThreadPool"为上面的执行器 --> <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
spring supports multithreading singleton
Why are local variables not affected by the impact of multi-threaded?
For those singleton class will run, such as Servlet Web multithreaded applications, each method of operation of a local variable is done in a thread of its own memory area, it is thread safe
Local variables are not affected by the impact multithreading
It will be multi-threaded member variables influence
For the operating member variables can be used to ensure the ThreadLocal thread safety
ThreadLocal define a member variable in the bean object, the variable needs of member variables stored in a ThreadLocal