? Some summary of Java concurrent programming %.1. Disadvantages of not applying the thread pool some developers figure it easy to get a new thread (...) in a place where multi-threading is required. Start () is not a problem for general scenarios, but it can be a bit of a risk if the concurrent requests are high:? The cost of the new thread. Threads are a lot lighter than processes, but for the JVM, the cost of creating a new thread is quite large, unlike creating a new object? Resource consumption. Without a pool to limit the number of threads, the number of threads will directly depend on the concurrency of the application, so the potential for thread data is huge, so resource consumption will be huge? stability. When the number of threads exceeds the level of system resources, stability can be problematic, suspended animation.
%.1. Develop execution strategy in each place where multi-threading is required, regardless of the amount of concurrency, the execution strategy of the thread needs to be considered. In what order does the task execute? How many tasks can be executed concurrently? How many tasks can you go into while the system is overloaded? How do I notify the application? What should be done before and after a task is executed
%.1. Thread pool Types Whether you create a thread pool through executors or manage it through spring, it's clear what kinds of thread pools are there:? Fixedthreadpool: Fixed thread pool, when submitting a task to create threads, until the maximum capacity of the pool, if the thread is not expected to end, will be supplemented by new threads? Cachedthreadpool: Variable thread pool, which is like a spring, if there is no task requirement, it reclaims the idle thread, if the demand increases, increase the thread on demand, do not limit the size of the pool? Singlethreadexecutor: Single Thread. Tasks that cannot be processed will go into the FIFO queue for execution? Secheduledthreadpool: A recurring thread pool. Support for performing recurring thread tasks in fact, these different types of thread pools are done by building a threadpoolexecutor, unlike Corepoolsize,maximumpoolsize,keepalivetime,unit, Workqueue,threadfactory so many parameters. Refer to JDK DOC for details.
%.1. Thread pool saturation policy by using the thread pool type, it is possible to have saturation in addition to the cachedthreadpool of other thread pools, when saturation requires the corresponding policy to process the task of the request thread, for example, Up to a limit the Threadpoolexecutor.setrejectedexecutionhandler method sets a policy for rejecting a task, and the JDK provides AbortPolicy, Callerrunspolicy, Discardpolicy, discardoldestpolicy Several strategies, specific differences visible JDK DOC
%.1. Thread non-dependent multithreading tasks are designed to make each task independent and dependent, so-called dependencies can be two aspects:? dependencies between threads. If a thread has dependencies it can cause deadlocks or starvation? The caller's dependency to the thread. The caller has to monitor the completion of the thread, affecting the concurrency amount of course, in some businesses do need certain dependencies, such as the caller needs to get the results of the thread completion, the traditional thread is inconvenient to complete, because the Run method has no return value, only through some shared variables to pass the result, But in the executor framework you can implement tasks that require a return value through the future and the callable, and of course the asynchronous nature of the threads leads to the need for mechanisms to ensure that callers can wait for the task to complete, as explained in the previous article on the future and callable usage;
HADOOP12---? Some summary of Java concurrent programming