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

  • Home
  • SEARCH
  • 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 6191479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:46:24+00:00 2026-05-24T02:46:24+00:00

I have one thread populating a vector object with values, and one other thread

  • 0

I have one thread populating a vector object with values, and one other thread that periodically fetches the values from it and clears it periodically.

I want that either thread accessing the vector pauses while the other one is accessing it. Do I need to use the wait/notify/notifyAll keywords ?

private Vector<Long> _recordIdsSent;

# Called by main thread (before thread A and thread B are started)
public ConstructorOfThisClass() {
    _recordIdsSent = new Vector<Long>();
    // Starts thread A & B
}

# Called by thread A
public void addSentRecordIds( List<Long> ids ) {
    synchronized (_recordIdsSent) {
        _recordIdsSent.addAll( ids );
    }
}

# Called by thread B
public void deleteRecords()
{
    List<Long> ids;
    synchronized (_recordIdsSent) {
        ids = (List<Long>) _recordIdsSent.clone();
        _recordIdsSent.clear();
    }

    // Delete the records matching ids....
}

Note: I clone the _recordIdsSent vector because the delete operation can take some time.

[EDIT]
Moved the synchronized keyword from the method signature to the variable _recordIdsSent

  • 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-24T02:46:25+00:00Added an answer on May 24, 2026 at 2:46 am

    As already noted, in provided code access to vector from addSentRecordIds is not synchronized – that makes your code unsafe

    Also, this code snippet does not guarantee that object used for locking (_recordIdsSent) doesn’t change – this makes it unsafe too. Myself I typically prefer dedicated objects for locking because it makes my intent cleaner and is less error prone. Like this:

    private final Object lock = new Object(); // dedicated lock object
    //... _recordIdsSent, addSentRecordIds etc...
    public void deleteRecords()
    {
        List<Long> ids;
        synchronized (lock) {
            ids = (List<Long>) _recordIdsSent.clone();
            _recordIdsSent.clear();
       }
    
       // Delete the records matching ids....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one thread that writes results into a Queue. In another thread (GUI),
I have one thread that is receiving data over a socket like this: while
I have one thread that inserts into the queueStream (not shown here) and FlowController
I have one background thread I use for processing separately from the GUI, and
I have one main thread that does some rather CPU intensive operation. The thread
I have already one thread that has to do following work: public class DetectionHandler
I have one std::list<> container and these threads: One writer thread which adds elements
I am writing code in VS2005 using its STL. I have one UI thread
I have two threads in an Android application, one is the view thread, and
I have a List object being accessed by multiple threads. There is mostly one

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.