Java concurrency-thread starvation deadlock test

Source: Internet
Author: User

Thread Starvation Deadlock

The explanation for thread starvation deadlocks in Java Concurrency Programming practice is that when you use a thread pool to perform a task, a deadlock problem can occur if the task relies on other tasks. In a single-threaded executor, if one task submits another task to the same executor and waits for the result of this committed task, it will inevitably lead to a deadlock. The first task is in the work queue and waits for the result of the second task, while the second task is in the waiting queue, waiting for the first task to be executed. This is the typical thread starvation deadlock. Even in multithreaded executor, if the tasks submitted to executor depend on each other, there may be a deadlock problem due to the insufficient number of worker threads.

Single-threaded executor, where tasks depend on each other and lead to deadlock test code is as follows: Define the Randerpagetask task, it will put another loadfiletask task to the same thread pool and wait for it to return, the final tragedy occurred.

Import Java.util.concurrent.callable;import Java.util.concurrent.executionexception;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.Future; public class Threaddeadlock {Executorservice exec = Executors.newsinglethreadexecutor ();/** * This task submits another task to the thread pool, And wait for the task to execute results * @author BH */public class Renderpagetask implements callable<string>{@Overridepublic String call () th Rows Exception {System.out.println ("Renderpagetask depends on the results returned by Loadfiletask task ..."); future<string> Header,footer;header = exec.submit (New Loadfiletask ("header.html")); footer = Exec.submit (new Loadfiletask ("footer.html")); String page = Renderbody (); return Header.get () +page+footer.get ();} Public String Renderbody () {return ' render body is OK. ';}} public static void Main (string[] args) {Threaddeadlock lock = new Threaddeadlock (); future<string> result = Lock.exec.submit (lock.new renderpagetask ()); try {System.out.println ("last Result:" + Result.get ());} catch (Interruptedexception | Executionexception e) {e.printstacktrace ();} Finally{lock.exec.shutdown ();}}}
Loadfiletask Task Code:

Import Java.util.concurrent.callable;public class Loadfiletask implements callable<string> {private String Filename;public loadfiletask (String filename) {this.filename = FileName;} @Overridepublic String Call () throws Exception {System.out.println ("Loadfiletask Execute call ..."); return fileName;}}
The deadlock problem can be avoided by replacing the single-threaded executor with an infinitely large thread pool.

Executorservice exec = Executors.newcachedthreadpool ();
The conclusion is that when the tasks submitted to the thread pool depend on each other, you need to be wary of deadlocks.

Resource deadlock when multiple threads share the same set of multiple resources, a deadlock can occur if a thread is waiting to acquire a resource while holding another resource. This is essentially similar to a scene in lock sequence deadlock, where a typical scenario for resource collections is a database connection pool. When there are multiple connection pools in different databases in the application, if the execution of the task requires the connection of two databases, the following can happen: A thread waits for the connection of the B database after holding a connection to the a database, and the other process has the opposite operation Flow and then falls into a deadlock situation. Therefore, when you use a thread pool to perform tasks, you should avoid the task of interdependent tasks being committed to the same thread pool.

Java concurrency-thread starvation deadlock test

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.