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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:53:25+00:00 2026-05-27T05:53:25+00:00

I have the following code public class Test { Lock lock = new ReentrantLock();

  • 0

I have the following code

public class Test {
Lock lock = new ReentrantLock();

public static void main(String args[]) throws Exception {
    Test t = new Test();
    Second second = t.new Second();
    second.lock = t.lock;
    Thread thread = new Thread(second);
    thread.start();
    Thread.sleep(2000);
    try {
        t.lock.lock();
        System.err.println("got the lock");
    } finally {
        second.shutdown = true;
        t.lock.unlock();
    }
}

private class Second implements Runnable {
    Lock lock;
    volatile boolean shutdown = false;
    int i = 0;

    public void run() {
        while (!shutdown) {
            try {
                lock.lock();
                System.out.println("In second:" + i++);
            } finally {
                lock.unlock();
            }
        }
    }
}

}

I read here that there is a concept of fair and unfair lock, but making locks fair has a big performance impact and nevertheless shouldn’t the above code give some fairness to the current thread.
While actual execution of the above code, the second thread runs forever (gave way for main thread after 545342 iterations)

Is there something I am doing wrong here? Can anyone explain this behavior?

  • 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-27T05:53:26+00:00Added an answer on May 27, 2026 at 5:53 am

    Basically without making the lock fair, the second thread is unlocking and managing to reacquire the lock before the first thread gets a chance to do so. After your large number of iterations, it must have been pre-empted between the “unlock” and the “lock”, giving your first thread an opportunity to get in and stop it.

    Fundamentally though, you simply shouldn’t have code like that in the second thread – under what real life situation do you want to repeatedly release and acquire a lock doing no work between the two, beyond checking a flag? (And if you do want to do that, why do you want to require that a “shutting down” thread acquires the same lock in order to set the flag?)

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

Sidebar

Related Questions

I have following code public class TEST { public static void main(String arg[]){ try
I have the following code: class Program { static void Main(string[] args) { Test
I have the following code: public class Test { public static void Main() {
I have the following code public class Test{ private static final String key =
I have the following code : using System.Collections.Generic; public class Test { static void
I have the following code: public class Test extends JFrame implements ActionListener{ private static
Let's assume I have the following code: public class MainClass { public static void
I have the following code(simplified). public class OrderProcessor { public virtual string PlaceOrder(string test)
I have the following java code: public class CheckInnerStatic { private static class Test
I have the following code: public class Foo {} static class Program { [XmlElement(foo)]

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.