Java Threads and thread pool (program ape must SEE)

Source: Internet
Author: User

before I saw someone else write a multi-threaded method, they used to do not know why, feel very strong at the same time and very curious. Java multithreading is a high-level must learn the knowledge, now the network on the query to the theoretical knowledge of special collation.
Simple Introduction
There are two ways to create a thread: inherit the thread or implement Runnable.  Thread implements the Runnable interface and provides an empty run () method, so you have to have your own run () method whether you inherit the thread or implement Runnable. Once a thread has been created, the start () method is called to run (Execute the Run () method), and the call to wait enters the sleep period, waits or calls sleep, and exits when it runs smoothly or when hibernation is interrupted or an exception occurs during the run. wait and sleep comparison:The Sleep method is: sleep (long Millis), sleep (long millis, long Nanos), after the sleep method is called, the current thread enters the dormant period, pausing execution, but the thread continues to have ownership of the monitoring resource. After the sleep time is reached, the thread continues execution until it finishes.  If the thread is interrupted by another thread during the sleep period, it exits. The wait method has: Wait (), wait (long timeout), wait (long timeout, long Nanos), after the wait method is called, the thread discards the ownership of the monitoring resource into the wait state; wait () : Wait for other threads to call notify () or Notifyall () to enter the dispatch state, competing with other threads for monitoring.  Wait () equals wait (0), wait (0, 0).  Wait (long timeout): The thread enters the dispatch state when another thread calls notify () or Notifyall (), or the time arrives at timeout, or there is another thread that interrupts the thread. Wait (long timeout, long Nanos): equivalent to wait (1000000*timeout + Nanos), except that the time unit is nanoseconds. the role of the thread pool:The thread pool function is to limit the number of threads executing in the system. According to the environment of the system, the number of threads can be set automatically or manually to achieve the best performance; less waste of system resources, more caused by the system congestion efficiency is not high. Use the thread pool to control the number of threads and other threads to wait. A task is completed, and then the first task from the queue is executed. If there is no waiting process in the queue, this resource of the thread pool is waiting. When a new task needs to be run, if there is waiting in the thread pool workThread, you can start running; otherwise, enter the wait queue. Why use a thread pool:Reduces the number of times a thread is created and destroyed, each worker can be reused, and multiple tasks can be performed to adjust the number of threads in the thread pool based on the system's ability to withstand, preventing the server from getting down because it consumes too much memory (about 1MB of memory per thread, the more threads open, The greater the memory consumed, the last crash) thread pool:Multithreading technology mainly solves the problem of multiple threads in the processor unit, which can significantly reduce the idle time of the processor unit and increase the throughput capacity of the processor unit.  Suppose a server takes a task to complete: T1 creates a thread time, T2 the time to execute a task in the thread, T3 destroys the threading time.  if: T1 + T3 is much larger than T2, you can use a thread pool to improve server performance. A thread pool consists of the following four basic components: 1, thread Pooling Manager (ThreadPool): Used to create and manage thread pools, including creating a thread pool, destroying a thread pool, adding new tasks, 2, worker threads (poolworker): Threads in thread pools, waiting when no tasks are available, Can loop the execution of tasks, 3, Task Interface (tasks): Each task must be implemented by the interface for the worker to dispatch the task of the execution, it mainly specifies the entry of the task, the completion of the task after the end of the task, the implementation of the status, etc. 4, Task Queue (Taskqueue): Used to hold tasks that are not processed.  Provides a buffering mechanism. Thread pooling technology is a technique that focuses on how to shorten or adjust t1,t3 time to improve the performance of server programs.  It t1,t3 the start and end of the server program, or some idle time period, so that there is no t1,t3 overhead when the server program processes the client request. The thread pool not only adjusts the time period generated by the T1,T3, but it also significantly reduces the number of threads created, for example: Suppose a server processes 50,000 requests a day, and each request requires a separate thread to complete. The number of threads in a thread pool is generally fixed, so the total number of threads does not exceed the number of threads in the thread pool, and the total number of threads is 50000 if the server does not use the thread pool to process these requests. The general thread pool size is far less than 50000. Therefore, server programs that utilize the thread pool do not waste time processing requests in order to create 50000, thereby increasing efficiency.


Java Threads and thread pool (program ape must SEE)

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.