Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:42:15+00:00 2026-06-18T04:42:15+00:00

//code taken from java concurrency in practice package net.jcip.examples; import java.util.concurrent.*; public class ThreadDeadlock

  • 0
//code taken from java concurrency in practice

  package net.jcip.examples;

import java.util.concurrent.*;


public class ThreadDeadlock
       {
    ExecutorService exec = Executors.newSingleThreadExecutor();

    public class LoadFileTask implements Callable<String> {
        private final String fileName;

        public LoadFileTask(String fileName) {
            this.fileName = fileName;
        }

        public String call() throws Exception {
            // Here's where we would actually read the file
            return "";
        }
    }

    public class RenderPageTask implements Callable<String> 
    {
        public String call() throws Exception
        {
            Future<String> header, footer;
            header = exec.submit(new LoadFileTask("header.html"));
            footer = exec.submit(new LoadFileTask("footer.html"));
            String page = renderBody();
            // Will deadlock -- task waiting for result of subtask
            return header.get() + page + footer.get();
        }


    }
}

This code is take from Java concurrency in practice and as per the authors “ThreadStarvtionDeadlock” is happening here. Please help me finding how ThreadStarvationDeadlock is happening here and where? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:42:16+00:00Added an answer on June 18, 2026 at 4:42 am

    Deadlock & Starvation is occurring at following line:

    return header.get() + page + footer.get();
    

    HOW?
    It will happen if we add some extra code to the program. It might be this one:

        public void startThreadDeadlock() throws Exception
        {
            Future <String> wholePage = exec.submit(new RenderPageTask());
            System.out.println("Content of whole page is " + wholePage.get());
        }
        public static void main(String[] st)throws Exception
        {
            ThreadDeadLock tdl = new ThreadDeadLock();
            tdl.startThreadDeadLock();
        }
    

    Steps that leading to deadLock:

    1. Task is submitted to exec for Rendering the page via Callable implemented class RenderPageTask.
    2. exec started the RenderPageTask in separate Thread , the only Thread that would execute other tasks submitted to exec sequentially .
    3. Inside call() method of RenderPageTask two more tasks are submitted to exec . First is LoadFileTask("header.html") and second is LoadFileTask("footer.html"). But since the the ExecutorService exec obtained via code Executors.newSingleThreadExecutor(); as mentioned here uses a single worker thread operating off an unbounded queueThread and the thread is already allocated to RenderPageTask , So LoadFileTask("header.html") and LoadFileTask("footer.html") will be en queued to the unbounded queue waiting for there turn to be executed by that Thread.
    4. RenderPageTask is returning a String containing the concatenation of output of LoadFileTask("header.html") , body of page and output of LoadFileTask("footer.html"). Out of these three parts page is obtained successfully by RenderPageTask . But other two parts can only be obtained after both tasks are executed by the single Thread allocated by ExecutorService . And Thread will be free only after call() method of RenderPageTask returns . But call method of RenderPageTask will return only after LoadFileTask("header.html") and LoadFileTask("footer.html") is returned. So Not letting LoadFileTask to execute is leading to Starvation . And each task waiting for other task for completion is leading to DeadLock

      I hope this makes clear of why thread starvation deadlock is occurring in above code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

// code exactly taken from java concurrency in practice public class TimingThreadPool extends ThreadPoolExecutor
This code snippet from java concurrency in practice, I really don't understand. @ThreadSafe public
I have wrote the code below which was taken from Java How to program
i have the following code, taken partly from a Red Black Tree Java implementation.
I am using this code taken from here : import smtplib def prompt(prompt): return
This is a small snippet of code taken from some of the examples that
The following code snippet is taken from android JellyBean ReferenceQueue.java in the libcore project.
I am using JCIFS (http://jcifs.samba.org/). My code is simple and taken from the Login.java
I'm trying to run this code taken from Sun Java site (I didn't copy
I'm implementing some code using the java.util.concurrency framework. I will be passing a collection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.