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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:45:35+00:00 2026-05-24T04:45:35+00:00

Thread A is summing up data passed from 10 clients. while(true){ Socket clientfd= server.accept

  • 0

Thread A is summing up data passed from 10 clients.

while(true){
    Socket clientfd= server.accept ();
    BufferedReader message = new BufferedReader(new InputStreamReader (clientfd.getInputStream() ) );
    String val = message.readLine();
    this.sum_data+=(message.readLine();
    message.close ();
    clientfd.close ();
    this.left--;
    if(this.left==0){
        System.out.println(this.sum_data);
        break;
    }
}

Thread B is constantly communicating with clients whether they are alive or not (heartbeating technique).

The thing is that clients sometimes can fail, and in that case, thread which is summing up data should just print out the all possible results from alive clients. Otherwise, it will never printout the result.

So, if heartbeat thread notices one client is not responding, is there a way for it to tell the other thread (or change other thread’s class variable this.left)?

  • 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-24T04:45:38+00:00Added an answer on May 24, 2026 at 4:45 am

    Basically, there are two general approaches to thread communication:

    1. Shared memory
    2. Event/queue based

    In the shared memory approach, you might create a a synchronized list or a synchronized map that both threads may read from and write to. Typically there is some overhead to making sure reads and writes occur without conflicts, you don’t want to have an object you’re reading deleted while you’re reading it, for instance. Java provides collections which are well behaved, like Collections.synchronizedMap and Collections.synchronizedList.

    In event, or queue based, thread communication, threads have incoming queues and write to other thread’s incoming queues. In this scenario, you might have the heartbeat thread load up a queue with clients to read from, and have the other thread poll/take from this queue and do its processing. The heartbeat thread could continually add the clients that are alive to this queue so that the processing thread “knows” to continue processing them.

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

Sidebar

Related Questions

I have a thread that needs to write data from an in-memory buffer to
I am writing multi-thread socket chat in C++Builder 2009. It is almost complete in
Thread MeasureThread = new Thread(delegate() { TextBlock tb = new TextBlock(); }); MeasureThread.Start(); This
Thread t = new Thread (WriteY); t.Start(); for (int i = 0; i <
Thread 1: is executing this loop while(running) { // Do Task() } println(Done); Thread
This thread shows how to open external sites in a new window and provide
The thread created by the following is the foreground thread Thread workingThread = new
Thread t1= new Thread(new Runnable() { public void run() { //perform Database stuff }
Using Thread is pretty straightforward Thread thread = new Thread(MethodWhichRequiresSTA); thread.SetApartmentState(ApartmentState.STA); How to accomplish
Starting a new thread... about same question.. I have tried all I am getting

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.