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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:11:15+00:00 2026-06-15T17:11:15+00:00

While passing SCJP6 exam simulator I found question like this: class Clerk implements Runnable

  • 0

While passing SCJP6 exam simulator I found question like this:

class Clerk implements Runnable {

    private Record A, B;

    public Clerk(Record a, Record b) {
        A = a;
        B = b;
    }

    public void run() {
        while(true) {
            doStuff(A, B);
        }
    }

    public synchronized void doStuff(Record a, Record b) {
        synchronized(a) {
        synchronized(b) {
            a.add(1);
            b.add(-1);
        }}
    }

}

then

Record a = new Record();
Record b = new Record();

new Thread(new Clerk(a, b)).start();
new Thread(new Clerk(a, b)).start();

Answer says that this code can cause deadlock, but I don’t get it – how exactly is that possible? Can someone can help me figure that out?

  • 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-15T17:11:16+00:00Added an answer on June 15, 2026 at 5:11 pm

    Apart from the fact that it does not compile, there is no deadlock in that code. This code could definitely create a deadlock:

    new Thread(new Clerk(a, b)).start();
    new Thread(new Clerk(b, a)).start();
    

    So if the question is: could the Clerk class be the source of the deadlock? Then the answer is yes.

    EDIT

    Short example that should deadlock fairly fast. If a and b are used like in the original question, the program runs fine.

    public class Test1 {
    
        public static void main(String[] args) {
            Record a = new Record();
            Record b = new Record();
    
            new Thread(new Clerk(a, b)).start();
            new Thread(new Clerk(b, a)).start();
        }
    
        static class Record {
        }
    
        static class Clerk implements Runnable {
    
            private Record A, B;
    
            public Clerk(Record a, Record b) {
                A = a;
                B = b;
            }
    
            public void run() {
                while (true) {
                    System.out.println("in thread " + Thread.currentThread());
                    for (int i = 0; i < 10000; i++) {
                        doStuff(A, B);
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                    }
                }
            }
    
            public synchronized void doStuff(Record a, Record b) {
                synchronized (a) {
                    synchronized (b) {
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yesterday while going through this question, I found a curious case of passing and
There is a problem executing this block of code while passing the parameter. Parameter
I am having issues while passing objects across Appdomains.During further investigation I found that
How can I instantiate an anonymous object while passing the propertynames and values as
i am using mvc 3 code first. facing problem while passing data form SecurityAttribute
Passing User defined argument to RPM is possible while installing?. for example: ~>rpm -i
What I want to do is while executing MySQL query, passing column data into
I got this error An error occurred while parsing EntityName. Line 1, position 61.
While passing data (Data Serialization) from Java (server) to Flex (client) the names of
I am trying use a javascript function while passing php variables in it. For

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.