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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:13:52+00:00 2026-06-12T19:13:52+00:00

I am trying to send request to server in every 2 seconds in a

  • 0

I am trying to send request to server in every 2 seconds in a thread and check if there is something for me give it back to me….For getting value i have to use callable. I am not able to figure out how to run callable thread in every 2 seconds and get value back from it…here is my sample code of callable implementation…

public String call(){
    boolean done = true;
    String returnData = "";
    while(done){
        try {
            returnData = post.getAvailableChat();
            if(!returnData.equals("")){
                System.out.println("Value return by server is "+returnData);
                return returnData;
            }
            return null;
        } catch (IOException ex) {
            done = false;
            Logger.getLogger(GetChatThread.class.getName()).log(Level.SEVERE, null, ex);
        }

Now here is my main class code i know i did it wrong here in main class because my code will not go to next line after while loop….but please tell me how to do it

Callable<String> callable = new CallableImpl(2);                  

    ExecutorService executor = new ScheduledThreadPoolExecutor(1);   
    System.err.println("before future executor");                    
    Future<String> future;                                           

    try {                                                           
        while(chatLoop_veriable){                                    
            future = executor.submit(callable);                         
            String serverReply = future.get();                      
            if( serverReply != null){                               
                System.out.println("value returned by the server is "+serverReply);
                Thread.sleep(2*1000);                               
            }//End of if                                            
        }//End of loop                                              
    } catch (Exception e) {                                         
  • 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-12T19:13:53+00:00Added an answer on June 12, 2026 at 7:13 pm

    You rightly picked a ScheduledThreadPoolExecutor but you don’t take advantage of the methods it provides, in particular in your case: scheduleAtFixedRate instead of submit. You can then remove the sleep part as the executor will handle the scheduling for you.

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

Sidebar

Related Questions

I have a problem when trying to send a POST request. The sending method
I'm trying to send a facebook request from the server side api (php). I
I am trying to send a POST request to my server, everything seems to
I'm working on a project where I'm trying to send request to our webservice
I wrote REST service using ASP.NET Web API. I'm trying to send HttpDelete request,
I am trying to send a ajax request to my views.py but I dont
I am trying to send a POST request to a rails scaffold controller which
I'm trying to send a SOAP request using SOAPpy as the client. I've found
I'm trying to send a post request to Parse: https://parse.com/docs/rest#objects-creating . Can't get it
I am receiving a NullPointerException when trying to send a POST request to a

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.