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 7436135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:11:27+00:00 2026-05-29T10:11:27+00:00

this the code in which i am trying to demonstrate nested lock problem, import

  • 0

this the code in which i am trying to demonstrate nested lock problem,

  import java.util.concurrent.locks.*;


  class SharedResource{
    private static final Lock lock = new ReentrantLock();


    private void methodThree(String name,int x) throws Exception{
        lock.lock();
        while(x <= 15){
            System.out.println("METHOD-THREE / THREAD-NAME : "+name+" NUM-VAL "+x);
            x++;
            Thread.sleep(250);
        }
    }

    private void methodTwo(String name,int x) throws Exception{
        lock.lock();
        while(x <= 10){
            System.out.println("METHOD-TWO / THREAD-NAME : "+name+" NUM-VAL "+x);
            x++;
            Thread.sleep(250);
        }
        methodThree(name,x);
    }

    public void methodOne(String name,int x) throws Exception{
        try{        
            lock.lock();
            while(x <= 5){
                System.out.println("METHOD-ONE / THREAD-NAME : "+name+" NUM-VAL "+x);
                x++;
                Thread.sleep(250);
            }   
            methodTwo(name,x);          
        }finally{
            lock.unlock();
        }       
    }

  }

  class MyRequestREQ extends Thread{

    private SharedResource res;
    private int num = 1;

    MyRequestREQ(SharedResource res,String name){
        super(name);
        this.res = res;
    }

    @Override
    public void run(){      
        try{
            res.methodOne(Thread.currentThread().getName(),num);
        }catch(Exception e){
            System.out.println(e);
        }
    }
  }

  public class LockCountPractise{
    public static void main(String [] args){
        SharedResource resource = new SharedResource();
        MyRequestREQ[] requests = new MyRequestREQ[]{
            new MyRequestREQ(resource,"JACK"),
            new MyRequestREQ(resource,"JILL"),
            new MyRequestREQ(resource,"JASON")
        };

        for(int x=0; x < requests.length;x++){
            requests[x].start();
        }
    }
  }

but the output which i get is all the one run by the thread “JACK”, this thread prints till count 15, and just hung up.

Is the above program face a deadlock issue ?

do i need to unlock the lock in all of the methods of class SharedResource ?

Waiting for the suggestion.

  • 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-05-29T10:11:28+00:00Added an answer on May 29, 2026 at 10:11 am

    you are not unlocking in method3 so when the first thread is done the others can’t go on since they can’t acquire the lock.

    do i need to unlock the lock in all of the methods of class
    SharedResource ?

    yes because every time you call lock():

    If the current thread already holds the lock then the hold count is
    incremented by one and the method returns immediately.

    see:
    http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html#lock%28%29

    as a suggestion: you can acquire the lock in method1 and release it in method3. so there will be 1 lock 1 unlock, you’ll be fine. no need to 3 lock-unlock cycle.

    actually it depends what behaviour you want:

    • To let different threads acquire the lock between counters (thread1 counts to 5 then thread3 comes and counts then thread1 continues) you need lock-unlock in every method.
    • To get one thread start and finish counting without any interfere you need 1 lock-unlock
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to compare two dates. I have this code which I thought
I am trying to understand this inline assembly code which comes from _hypercall0 here
I have this php code, with which I am trying to generate a popup
i am trying to use this code: <%= File.ReadAllText(Server.MapPath(Members/newsletters/welcome.html))%> which works great but now
I have this code which compiles and works as expected: class Right {}; class
I'm trying to loop some array using foreach . This is the code which
I just encountered this (made up code to demonstrate the problem): public ICollection<string> CreateCollection(int
I'm trying make this code which is from a Dictionary of Arrays (CODE) from
I use this code which is taken from MVC futures and attach the Attribute
I have this code which is called at an onChange event of an function

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.