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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:37:20+00:00 2026-06-17T05:37:20+00:00

I have an issue which I hope I will solve by writing this question

  • 0

I have an issue which I hope I will solve by writing this question but if not I will post and see if anyone can help.

I am using a client library (which is poorly written I feel) to interact with a real time chat server that utilises COMET style long-polling over HTTP. I’m having issues with cancelling the long-poll in certain situations and suspect I may need to add some concurrency handling code but am finding it hard to work out the best way to do this for the below reasons.

The subscribing code (that inits the long poll) is implemented as a big loop with the following

doLongPoll()
{
    while(true)
    }
        //IF channel field boolean unsubscribe == TRUE, if so BREAK;
        //perform GET request (and store channel HTTPClient used for this call)
        //remove HTTPClient used for this call
        //IF channel field boolean unsubscribe == true, if so BREAK;
        //IF connection problem sleep(1500) then CONTINUE
        //post received data to listeners
    }
}

The unsubscribe call (that will called on another thread)

unsubscribe()
{
    //set channel field boolean unsubscribe == FALSE
    //get channel HTTPClient and shutdown
}

I have isolated the problem case where the operations are interleaving. To me this seems a result of the code being multi-threaded and the client code not being thread safe. Also poor management and non-reuse of the httpClient is not helping.

One of the issues I’m having is below where the unsubscribe call does not stop the next getRequest from taking place.

THREAD 1 (polling)                      THREAD 2
--------                                --------
do unsubscribe check (pass)
                                        unsubscribe called
                                        set unsubscribe = true
                                        check if httpClient saved (none)
perform getRequest (save HttpClient first)

I would like to know what people think the best approach to this problem would be (time is also limited so I cant rewrite too much of the code!)

To fix this i would thought i could use a syncronisation block from the first unsubscribe check on thread 1 up to the point the httpClient is saved just before the actual get request is performed and syncronise the unsubscribe method using the same lock. This is not practical at the moment as the first sync block mentioned would start in one method call and finish further down the method call chain (due to how the lib is written) – which feels very wrong so some refactoring would be needed.

OR I could just create a single httpClient per channel rather than per request and then it could always be shutdown and I could potentially ignore the syncronisation (i think).

OR as suggested below I could use interrupts for the same purpose

Any suggestions would be welcome – I will edit if I have any progress!

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-17T05:37:22+00:00Added an answer on June 17, 2026 at 5:37 am

    I bought Java Concurrency in Practice as a result of this problem and found they discuss an issue very much like this one in Chapter 7: Cancellation and shutdown which can be summarised by the quote

    Interruption is usually the most successful way to implement
    cancellation

    As the HttpClient is blocking socket IO that does not support interruption so I have a two pronged approach. I allocate my httpClient, I check for interruption just before the actual httpClient.execute() call and in my unsubscribe() method interrupt the thread and then call httpClient.getConnectionManager().shutdown();. This seems to take care of my problem and was a very simple change. No more interleaving issues!

    I also set the boolean unsubscribe field to volatile as suggested which I shouldve done before – this alone however would not have solved the issue

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

Sidebar

Related Questions

I think and hope this is a minor issue. I have a JFrame which
I really hope you can help me solve this issue.. I am trying to
This is my first question here, but I hope I will get a lot
I have an issue which I could not find answer for across the web.
I have some urgent issue which I could not find answer for across the
I'm pretty new in iphone programming and have stumbled upon this issue which I
I am fairly new to C++, but i have ran into an issue which
I have a requirement like the following but can't solve it and that's why
I hope this is not overly convoluted. I have tried searching through the internet
I have made a question which is a bit similar to this, though the

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.