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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:22:51+00:00 2026-06-13T18:22:51+00:00

I am writing a multithreaded program and I have a method that has a

  • 0

I am writing a multithreaded program and I have a method that has a nested synchronized blocks and I was wondering if I need the inner sync or if just the outer sync is good enough.

public class Tester {

    private BlockingQueue<Ticket> q = new LinkedBlockingQueue<>();
    private ArrayList<Long> list = new ArrayList<>();

    public void acceptTicket(Ticket p) {
        try {
            synchronized (q) {
                q.put(p);

                synchronized (list) {
                    if (list.size() < 5) {
                        list.add(p.getSize());
                    } else {
                        list.remove(0);
                        list.add(p.getSize());
                        }
                }
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

EDIT:
This isn’t a complete class as I am still working on it. But essentially I am trying to emulate a ticket machine. The ticket machine maintains a list of tickets in the BlockingQueue q. Whenever a client adds a ticket to the machine, the machine also keeps track of the price of the last 5 tickets (ArrayList list). So I don’t believe I need the inner sync because its is only this class (the ticket system) that will access the list

  • 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-13T18:22:52+00:00Added an answer on June 13, 2026 at 6:22 pm

    The danger with:

        synchronized (q) {
            synchronized (list) {
             }
        }
    

    Is one day you’ll add a method, or call methods in such an order that in effect it does this as well:

        synchronized (list) {
            synchronized (q) {
             }
        }
    

    Then it’s just a deadlock timebomb.

    Asuming this is the complete class, you can just lock one, or as is common, lock the object itself with synchronized on the method. All other access to these privates will need to be synchronized too.

    public class Tester {
    
    private BlockingQueue<Ticket> q = new LinkedBlockingQueue<>();
    private ArrayList<Long> list = new ArrayList<>();
    
    public synchronized void acceptTicket(Ticket p) {
        try {
          q.put(p);
    
          if (list.size() < 5) {
            list.add(p.getSize());
          } else {
            list.remove(0);
            list.add(p.getSize());
          }
        } catch (InterruptedException ex) {
            Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
        }    
    }
    

    Or cleaner:

    public class Tester {
    
    private BlockingQueue<Ticket> q = new LinkedBlockingQueue<>();
    private ArrayList<Long> list = new ArrayList<>();
    
    public void acceptTicket(Ticket p) {
        try {
          //this is cleaner, because I don't know what logger class is doing,
          //I want to eliminate chance of deadlock and reduce time we are in lock
          synchronized (this){
            q.put(p);
    
            if (list.size() < 5) {
              list.add(p.getSize());
            } else {
              list.remove(0);
              list.add(p.getSize());
            }
         }
        } catch (InterruptedException ex) {
            Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
        }    
    }
    

    Cleaner still, but is unlikely to be nessasary in your case (based on what I have seen):

    public class Tester {
    
    private final Object lockObj = new Object(); //specific object for locking
                                    //could use any other private, non-exposed final but
                                    //this makes it absolutely clear what I should be
                                    //using for locks
    private BlockingQueue<Ticket> q = new LinkedBlockingQueue<>();
    private ArrayList<Long> list = new ArrayList<>();
    
    public void acceptTicket(Ticket p) {
        try {
          //"this" can be locked by external code outside my control here,
          //so I use a specific private final object lockObj to eliminate deadlocks and
          //provide finer grained locking - reducing contension
          synchronized (lockObj){
            q.put(p);
    
            if (list.size() < 5) {
              list.add(p.getSize());
            } else {
              list.remove(0);
              list.add(p.getSize());
            }
         }
        } catch (InterruptedException ex) {
            Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multithreaded openCV program that uses 4 threads to do the following:
I am writing a multithreaded program in java. I have written something like this
Let's say I have a multithreaded C++ program that handles requests in the form
I have a client program as follows and I need to make it multithreaded
I am writing a multithreaded C program and I have an error. I have
I'm writing a multithreaded Java program where each thread potentially needs its standard output
I am writing a simple multithreaded socketserver and I am wondering how best to
I am writing a multithreaded program where I want to handle a possible Ctrl-C
I am writing a multithreaded program in which i am getting exception java.lang.IllegalThreadStateException .
I'm writing a multithreaded program, which would crash when a particular exception was thrown.

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.