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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:25:29+00:00 2026-06-12T02:25:29+00:00

I have a Java client (client server app) that does two main things: (1)

  • 0

I have a Java client (client server app) that does two main things: (1) listen to server to receive files and save them to a local folder and (2) watch that folder for changes and send changes to the server. I want to run each in its own thread. First, is it a good idea to run each task on a separate thread. Second, how do I lock the folder when it’s used by either task to avoid interference?

  • 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-12T02:25:30+00:00Added an answer on June 12, 2026 at 2:25 am

    is it a good idea to run each task on a separate thread

    It sounds like a good idea to split your program into threads since the 2 tasks can work asynchronously and concurrently. The 1st thread could be downloading at the same time the 2nd one is uploading.

    how do I lock the folder when it’s used by either task to avoid interference?

    I wouldn’t do a lock at all. I’d have your 1st thread read a file from the server, write it into the folder, and then add a FileToSend object (or maybe just a File object) to a BlockingQueue. So instead of looking at the directory, your 2nd thread would just be waiting on the BlockingQueue for files to be sent to the server. The LinkedBlockingQueue class should work well for this. The BlockingQueue takes care of the locking for you.

    If you do need to share a lock then you could just inject a lock object into your two threads:

    private final Object folderLock = new Object();
    ...
    new Thread(new Downloader(folderLock)).start();
    new Thread(new Uploader(folderLock)).start();
    ...
    

    A good pattern would be to define an addFileToUpload(File fileToUpload) method on your Uploader class. Then your Uploader can decide what to do with it. The BlockingQueue could then be local to the Uploader

    Uploader uploader = new Uploader();
    // pass the uploader into the downloader
    new Thread(new Downloader(uploader)).start();
    new Thread(uploader).start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have client-server app. Client on C++, server on Java. I am sending byte-stream
I have the following Java socket client app, that sends same string to socket
I have a client app that communicates to the spring server with REST type
I have an iPhone app that uses HessianKit to talk to my java server.
I have developed a java client server app based on socket programming. I am
I have developed a java swing client-server application. The server has many services like
Background: I have written a java swing based client server application. The server is
I have a client and server communicating via Spring remoting (using Java Serialization) over
I have created a server in java which accepts client connections. But I am
Question:- Does java client need to worry about multiple servers ? Meaning:- I have

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.