Turn to talk threads and connection pool

https://www.cnblogs.com/imyalost/p/7189455.html

 

Previous blog, there is a common term when it comes to performance testing: the connection pool. Which briefly about the role of the connection pool, etc., this blog will introduce the principle under connection pool and connection pool thread object, the role and advantages. . .

 

First, the connection pool

1. What is the connection pool? Why do we need it?

Connection pooling allows multiple clients to use cached connection objects, which can be connected to the database, they are shared, can be reused.

Open / close a large database connection overhead, connection pooling technology allows us to maintain a connection object connection pool, so you can improve the performance of the database to execute the command. A plurality of client requests can reuse the same connection object, when a client requests each time they receive,

You will search the connection pool to see if there is no idle connection object. If not, either all client requests are queued into the queue, or create a new connection object (depending on how many connections there is an existing pool and configure how much support connections) in the pool.

Once a request is finished using the connection object, the object will be back into the pool and then will be re-assigned to the queued requests (which request What assigned to the scheduling algorithm depends on use).

Because most requests are to use the existing connection object, so the connection pool technology greatly reducing the waiting time to create a database connection, thereby reducing the average connection time.

2, How to use connection pooling?

Connection pooling is common in web-based enterprise applications, the application server is responsible for creating the connection object, add them to the pool, the connection object assigned to the request, recovered after use connection objects, put them back again to the connection pool.

When the network application creates a database connection, the application server will remove the connection object from the pool, and when it is used up after a shut down, the application server is also responsible for the complete use of the connection object back into the pool.

PS: You can also use JDBC 1.0 / JDBC 2.0 API to obtain the physical connection (physical connnection), but this is rarely the case, because the database is connected only once, the case does not require connection pool.

3, the connection pool can handle the number of connections? Who created / release the connection?

The maximum number of connections can be configured, the minimum number of connections, the maximum number of idle connections, etc. All of these parameters can be configured by the server administrator. When the server starts, a fixed number of connection objects (the minimum number of connection configuration) is created and added to the pool.

When a client requests consumed all connected objects, no new requests will create a new connection objects, they are added to the pool and then assigned to this new request, the maximum number of connections until set.

Server will always view objects idle connections, when detecting the number of connections exceeds the set value idle, the server will close the connection is idle, they will then be garbage collected.

4, a conventional connection pool managed connection pool vs

Connection pooling is an open concept, any application can use this concept, and with the way you want to manage it. The concept refers to the connection pool to create, manage and maintain the connection object.

But when the scale increases, if there is no connection pooling mechanism a robust, it will have to manage the connection is increasingly difficult applications.

Therefore, the establishment of a robust, managed connection pool is necessary.

PS : About the contents of the connection pool, making reference to http://www.importnew.com/8179.html

 

Second, the thread pool & thread connecting the connection pool &

Thread : a program execution unit performs minimum unit stream, a physical process, a relatively independent, can be scheduled, the system is independent of the basic unit of scheduling and dispatching;

Multi-threading technology, refers to a process which can create multiple threads "simultaneously" to process multiple transactions;

Thread Pool : it can be understood as a buffer, because the destruction of threads created frequently will bring some costs, pre-created, but not destroyed immediately, in order to share ways to serve others, one can provide efficiency, furthermore can control thread wireless expansion.

Connection : means connected to the other bit point;

Connection Pooling : with thread pool has the same beauty, but the connection pool can be achieved can also be achieved through multi-process, it may be based on a single instance of multi-threading.

for example:

Socket at the time as a service, you can simultaneously monitor multiple client connections, its implementation principle is a bit like "connection pooling"; each customer simultaneously send data to the server via multiple ports, can be considered to be multi-threaded,

The server may have established a good n threads to wait while processing / analyzing data sent by the client, there may be a "thread pool."

 

Several state Third, thread

Thread under certain conditions, the status will change. Thread a total of the following states:

1, the new state (New) : create a new thread object.

2, ready state (Runnable) : After the thread object is created, other thread calls the start () method of the object. The state of the thread in the "runnable threads Pool", becomes operational, only waiting to acquire the right to use the CPU,

   That is in the process of readiness in addition to CPU, additional resources are needed to run all available.

3, running state (Running) : ready state of the thread gets the CPU, executes the program code.

4, blocking state (Blocked) : the thread is blocked for some reason to give up the right to use CPU temporarily stops running. Until the thread into the ready state, a chance to go running.

   Case of obstruction of three categories:

① wait obstruction: a thread of execution wait () method, the thread releases all resources occupied, JVM will thread into the "wait pool". After entering this state, it can not be automatically wake up,

   Must rely on other thread calls notify () or notifyAll () method to wake up,

. ② synchronous blocking: threads running at the time of acquiring synchronization lock object, if the synchronization lock is occupied by another thread, the thread into the JVM will "lock pool".

③ Other blocking: a thread of execution sleep () or join () method, or issue the I / O request, the JVM will set the thread is blocked. When the sleep () timeout, join () waits for a thread to terminate or a timeout,

   Or I / O processing is completed, the thread into the ready state again.

5, the death state (Dead) : thread execution is over or due to abnormal exit the run () method, the thread end of the life cycle.

   Thread changes state transition diagram as follows:

PS : get the lock object mark, that is to obtain a permission to use the object (critical area) is. That is the thread to get the resources needed to run, into the "ready state", just to get CPU, you can run.

    Because when calling wait (), the thread will be freed in its possession "lock symbol", so in this thread only access to resources in order to enter the ready state.

As explained below under: 

. ① implement threads in two ways, one is the Thread class inheritance, second is to achieve Runnable interface, but in any case, when we have a new object, the thread into the initial state; 

. ② When the object calls the start () method, they enter the ready state; 

③ After the ready, when the object is selected by the operating system, CPU time slice will get into operation.; 

. ④ After entering the operational state of the situation is more complicated;

   After (1) run () method or the main () method, the thread proceeds to the final state; 

   (2) When a thread calls its sleep () method or join other thread () method, the process allows the CPU, then it will enter the blocking state (the state not only stop the current thread, but it does not release any resources,

       That is, after calling sleep () function, the thread will not release its "lock flag." ). When sleep () or end join () ends, the thread becomes runnable, OS continues to wait for allocation of CPU time slice;

       Re-test after test found that the conditions are not satisfied, let the thread is blocked for some time until the conditions are met; typically, sleep () is used in the case of waiting for a resource is ready.

   (3) thread calls yield () method, which means giving up CPU time slice currently available, return to the ready state, then compete with other processes in the same state, OS there may be subsequently conceded this process into operation;

       Call yield () is equivalent to the effect of the thread scheduler think enough time has been performed piece thus need to go to another thread. yield () Causes the current thread just returned to an executable state,

       So thread executing yield () is likely to be executed immediately after entering into an executable state.

   (4) when the thread just runnable (note, not running), was found to be invoked resources synchroniza (synchronous), can not get a lock mark will immediately enter the locked state of the pool, waiting to acquire a lock mark

      (Then lock the pool may have been other threads are waiting to acquire the lock mark, then they are in the queue status, both first-served basis), once the thread gets the lock token, it is transferred to a ready state, waiting for allocation of CPU OS time slice.

   (5) suspend () and resume () method: supporting the use of two methods, suspend () so that the thread into the blocked state and does not automatically resume, which must correspond to the resume () is called, in order to make the thread re-enter the executable status.

       Typically, suspend () and resume () is used in another thread waiting for the results generated by the case: after tests found no results yet, so the thread blocked, another thread to produce results, call resume () to restore . 

   (6) wait () and notify () method: When the thread calls the wait () method will enter the queue (to enter the state will release all occupied resources, and different blocking state), after entering the state, it is not automatically wake up,

       Must rely on other thread calls notify () or notifyAll () method to wake up (because the notify () just wake up one thread, but we made not determine the specific wake of what a thread, perhaps we need to wake up the thread can not be awakened,

       Therefore, in actual use, usually with notifyAll () method, has awakened thread), the thread wakes up will enter the lock pool, waiting to acquire a lock mark. 

       wait () so that the thread into the blocked state, which has two forms:

       That allows the specified time in ms as a parameter, no other parameter. When the corresponding thread former notify i.e. re-enter the ready state READY state () is called or the specified time is exceeded, the latter must correspond to notify () is called.

       When the call to wait (), the thread will be freed in its possession "lock flag" so that other synchronized data resides in the target thread can be used by other threads.

       waite () and notify () because the "lock flag" would object to operate, they must be called in synchronized function or synchronizedblock in.

       If you call a function in non-synchronized or non-synchronizedblock, although able to compile, but at runtime IllegalMonitorStateException the exception occurs.

PS : About several state of the thread, reprinted from the open source China: several state of the thread

 

Guess you like

Origin www.cnblogs.com/exmyth/p/12617059.html