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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:24:27+00:00 2026-05-13T15:24:27+00:00

I have an archive object which manages various byte arrays and hands out InputStream

  • 0

I have an archive object which manages various byte arrays and hands out InputStreams and OutputStreams for reading and writing them. Each byte array has an associated ReentrantReadWriteLock.

The constructor of the subclass of InputStream which the archive produces acquires the lock for the relevant data, while close() releases the lock. Now, the problem:

Suppose that I have a task which will run on another thread which needs input from the archive, is given the InputStream as an argument before it starts, and is responsible for closing the stream when it’s finished. These requirements appear to be incompatible with the locking mechanism used by my data archive, because

  1. The InputStream is created on a different thread from the one on which it is closed.
  2. A ReentrantReadWriteLock must be released by the thread which owns it.

Rewriting the parts of the program which expect an InputStream as input in order to avoid #1 is inconvenient and makes those parts less flexible. Using a kind of lock which permits change of ownership would let me avoid #2, but I don’t see any locks in the Java API which can handle that and I’m not terribly keen on knocking together a custom lock if I don’t have to.

What solutions are there to this problem?

  • 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-13T15:24:27+00:00Added an answer on May 13, 2026 at 3:24 pm

    The cleanest way to do this is to give the clients something other than a subclass of InputStream, even if this means slightly more refactoring. In your current design, you can’t count on your clients releasing the lock, what if one of them does not? This is not good encapsulation.

    Have the archive object manage the locks, and provide an easy API for your clients to get the data. Even if this means changing some APIs.

    The problem of different threads locking and releasing the same object is a clear sign that you got something slightly wrong in your current design. Work on the refactoring aforementioned, it will do you a great deal further on.

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

Sidebar

Related Questions

I have a small problem, which might me a stupid mistake on my side.
I have a Winform DataGridView , which has a number of predefined columns. Now
I have a web service (JAX-RS/Spring) that generates SQL queries which run against a
I have the following problem : several object files a1.o, a2.o, a3.o, ... an
Ok, So I have a situation, where an border is being scaled (sometimes by
I am getting a Fatal error: Using $this when not in object context in
I have this problem: When I try to implement Ayende's complex searching found at:
I have a file in JSON format with record for individual users. Some of
I'll start with an example snippet: self.addwidget({ box: ns.box({ text: 'Foobar', fromTop: ~~(Math.random()*window.innerHeight), fromLeft:
Simple case: <usercontrol> <Views:UserListView x:Name=settingsTreeView/> <Button DataContext={Binding ElementName=settingsTreeView, Path=SelectedItem} Command={Binding CreateChildCommand}/> </usercontrol> The task

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.