Thread Starvation Deadlock

Posted By : Rajat Kukrety | 31-Oct-2018
Limited string pools enable the software engineer to indicate a maximum point of confinement on the number of strings that can simultaneously execute in a string pool. Projects must not utilize strings from a limited string pool to execute errands that rely upon the fulfilment of different undertakings in the pool. 
 
A type of stop called string starvation halt emerges when every one of the strings executing in the pool is hindered on undertakings that are looking out for an interior line for an accessible string in which to execute. String starvation gridlock happens when presently executing errands submit different assignments to a string pool and sit tight for them to finish and the string pool does not have the ability to suit every one of the undertakings on the double. 
 
This issue can be befuddling in light of the fact that the program can work accurately when fewer strings are required. The issue can be moderated, sometimes, by picking a bigger pool measure. Notwithstanding, deciding an appropriate size might be troublesome or even unthinkable. 
 
Correspondingly, strings in a string pool may neglect to be reused when two executing undertakings each require the other to finish before they can end. A blocking activity inside a subtask can likewise prompt unbounded line development
 
Below is the given code to simulate the same
 
//The main pool executor which will add all the tasks.
 
ExecutorService executorService = Executors.newFixedThreadPool(100);
 
 
// This will create the list of task for the pool and will invoke each task concurrently
 
void getMajortask(){

        List<Callable<String>> allMinorTask = new ArrayList<>();

        for(int i=0; i<10; i++){
            allMinorTask.add(getMinorTask());
        }
        List<Future<String>> minorResult =null;
        try {
            minorResult = executorService.invokeAll(allMinorTask);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        minorResult.forEach( i-> {
            try {
                i.get();
            } catch (InterruptedException | ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        });

    }
The Major task will create a mini task to add into the Queue to be executed by the same thread pool
 
 

Callable getMinorTask(){

    return ()->{

        List<Callable<String>> allMinorTask = new ArrayList<>();

        for(int i=0; i<10; i++){
            allMinorTask.add(getMinitask());
        }
        List<Future<String>> minorResult =null;
        try {
            minorResult = executorService.invokeAll(allMinorTask);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        minorResult.forEach( i-> {
            try {
                i.get();
            } catch (InterruptedException | ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        });
        return "-";

    };

}

Callable getMinitask(){

     Callable<String> c = () ->{

        return " Done ";
    };

    return c;
}
 
 
This will result in the pool to go in the Starvation state for all of the above threads

About Author

Author Image
Rajat Kukrety

Rajat is a bright Lead Java developer with sound knowledge in JAVA, Spring Other than programming his area of interest are listening music and reading novels.

Request for Proposal

Name is required

Comment is required

Sending message..