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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:39:22+00:00 2026-05-21T11:39:22+00:00

So i used the following to create a lock on a file so that

  • 0

So i used the following to create a lock on a file so that I can edit it exclusively:

 File file = new File(filename);
 channel = new RandomAccessFile(file, "rw").getChannel();
 lock = channel.tryLock();

Now I have a 2nd thread want to access the same file – just to read, not edit. How do i do that? Right now the 2nd thread will throw an io exception and inform me the file is locked.

Is this doable? Any suggestions? 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-05-21T11:39:23+00:00Added an answer on May 21, 2026 at 11:39 am

    You could try asking for a shared lock using the three argument version of tryLock.

    Here is the appropriate javadoc:
    http://download.oracle.com/javase/1.4.2/docs/api/java/nio/channels/FileChannel.html#tryLock%28long,%20long,%20boolean%29

    Basically instead of doing
    lock=channel.tryLock()
    you would do lock = channel.trylock(0, Long.MAX_VALUE, true)

    As an aside, you should be careful with file locking in java. While you can guarantee the locks behave as expected within the JVM you can’t necessarily be sure that they will behave as expected accross multiple processes.

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

Sidebar

Related Questions

I used the following code to create a file: FileOutputStream fos = app.openFileOutput(fileName, Context.MODE_WORLD_WRITEABLE);
I have used following code to create a simple PDF file. It executes fine
I think the following code can be used to create manipulators. #include<iostream> ostream &
i have used the following headers in order to create xls files that contains
I have successfully used the following command line to create a new GIF image
I have used the following code to create a new thread: [NSThread detachNewThreadSelector:@selector(backgroundMethod:) toTarget:self
I have used the following code to create a file in a temp location
I used following JS code to create equal height columns: var colHeight = Math.max($('.3columngallery
I have the following used to create a dummy high score table if if
I've always used the following codes to create a folder on a Apache server

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.