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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:11:47+00:00 2026-06-18T09:11:47+00:00

Consider you have a shared memory (List) which will serve as the critic section.

  • 0

Consider you have a shared memory (List) which will serve as the “critic section”.
Now, consider you that you always have items in the list for these scenarios and you want that your system will behave this way:

  1. Thread1 get some item from the list, in the very same time Thread2 wants to add item to the list. Allow this scenario( in assumption I will take first item from begining and insert the new item in the end of the list – in the SAME TIME!).

  2. Thread1 wants to get an item and in the same time Thread2 wants to get an item too.
    This should fail.

THANKS

  • 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-18T09:11:48+00:00Added an answer on June 18, 2026 at 9:11 am

    One possibility is to wrap your List in a class that proxies or overrides the get and add methods.

    That way, you can use an explicit Lock on the add method, so that only one thread can add at any given time.

    See for instance:

    http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html

    You could do this either by extending a List implementation, and overriding the add and get methods (or all relevant methods), or by using composition instead of inheritance, having a proxy class that forwards the calls to the list, but decorates the add and get with the explicit obtaining of the Lock.

    A very simple example would be something like:

    public class SharedMemory<K> {
    
    private final List<K> memoryList;
    private static final ReentrantLock lock = new ReentrantLock();
    
    public SharedMemory() {
        memoryList = new ArrayList<>();
    }
    
    public void storeItem(K item) {
        memoryList.add(item);
    }
    
    public K getItem(int pos){
        lock.lock();
        try{
            return memoryList.get(pos);
        } finally {
            lock.unlock();
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider I have a shared function:- Public Shared Function CalculateAreaFromRadius(ByVal radius As Double) As
Consider I have an array of elements out of which I want to create
Consider I have a table with notes which could be associated with zero or
Consider I have an anchor which looks like this <div class=res> <a href=~/Resumes/Resumes1271354404687.docx> ~/Resumes/Resumes1271354404687.docx
Consider I have a property named sampleObject. In dealloc method, which should be the
I have a class whose instances have attributes that are containers which themselves contain
I was implementing some simple Producer/Consumer program that had some semaphores and shared memory.
I have what I'd consider a relatively simple Highcharts chart that I want to
Consider a scenario in which two applications have to share data among them. I
I have question regarding shared memory segmentation in c using POSIX system calls. Is

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.