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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:32:30+00:00 2026-06-03T06:32:30+00:00

I was going through the advanced threads section that java offers regarding locks,I have

  • 0

I was going through the advanced threads section that java offers regarding locks,I have developed the code..

// A simple lock example.
import java.util.concurrent.locks.*;

class LockDemo {
    public static void main(String args[]) {
        ReentrantLock lock = new ReentrantLock();
        new LockThread(lock, "A");
        new LockThread(lock, "B");        
    }
}

// A shared resource.
class Shared {
    static int count = 0;
}

// A thread of execution that increments count.
class LockThread implements Runnable {
    String name;
    ReentrantLock lock;

    LockThread(ReentrantLock lk, String n) {
        lock = lk;
        name = n;       
        new Thread(this).start();
    }

    public void run() {
        System.out.println("Starting " + name);
        try {
            // First, lock count.
            System.out.println(name + " is waiting to lock count.");
            lock.lock();
            System.out.println(name + " is locking count.");
            Shared.count++;
            System.out.println(name + ": " + Shared.count);
            // Now, allow a context switch -- if possible.
            System.out.println(name + " is sleeping.");
            Thread.sleep(1000);
        } catch (InterruptedException exc) {
            System.out.println(exc);
        } finally {
            // Unlock
            System.out.println(name + " is unlocking count.");
            lock.unlock();
        }
    }
}

My query is that as we have seen above in class LockDemo that we are calling

new LockThread(lock, "A"); 
new LockThread(lock, "B");

can’t it be expressed in simple and full terms in coding so that I can grasp more please advise how to express it more simple forms.

  • 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-03T06:32:31+00:00Added an answer on June 3, 2026 at 6:32 am

    It always pisses me off when I see threaded code like this. You are right, it’s a bad example and what it does is not as obvious as it should be. The part that annoys me is the implementation of Runnable using itself to start a thread as well as the fact that it starts the thread inside the constructor.

    I would rewrite it like this:

    // A simple lock example.
    import java.util.concurrent.locks.*;
    
    class LockDemo {
        public static void main(String args[]) {
            ReentrantLock lock = new ReentrantLock();
            LockThread lt1 = new LockThread(lock, "A");
            LockThread lt2 = new LockThread(lock, "B");
            Thread t1 = new Thread(lt1);
            Thread t2 = new Thread(lt2);
            t1.start();
            t2.start();
        }
    }
    
    // A shared resource.
    class Shared {
        static int count = 0;
    }
    
    // A thread of execution that increments count.
    class LockThread implements Runnable {
        String name;
        ReentrantLock lock;
    
        LockThread(ReentrantLock lk, String n) {
            lock = lk;
            name = n;       
        }
    
        public void run() {
            System.out.println("Starting " + name);
            try {
                // First, lock count.
                System.out.println(name + " is waiting to lock count.");
                lock.lock();
                System.out.println(name + " is locking count.");
                Shared.count++;
                System.out.println(name + ": " + Shared.count);
                // Now, allow a context switch -- if possible.
                System.out.println(name + " is sleeping.");
                Thread.sleep(1000);
            } catch (InterruptedException exc) {
                System.out.println(exc);
            } finally {
                // Unlock
                System.out.println(name + " is unlocking count.");
                lock.unlock();
            }
        }
    }
    

    Now it’s much more clear from main that you are creating two thread tasks (objects whose class implements Runnable) and using them to start two threads.

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

Sidebar

Related Questions

I have been going through the threads regarding the spinners and when the onItemSelected
I have been going through and re-creating Apple's Advanced Table View Cells example to
I'm interested in becoming more fluent in Java so I have been going through
Going through the microsoft authentication tutorial listed here they have you create a master
Just going through the sample Scala code on Scala website, but encountered an annoying
While going through Effective C++ (by Scott Meyers), I came across the following code
i'm experimenting a bit with C# and XNA. Going through the advanced Riemers tutorials
While going through the iOS-4 Multitasking for fast context switching, I have a doubt
While going through the exercises, I came across something that, even after research, I
I've finished a personal project now just going through my code cleaning things up.

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.