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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:43:02+00:00 2026-05-21T17:43:02+00:00

Java newbie here. I am using a third party library which gives me a

  • 0

Java newbie here. I am using a third party library which gives me a function, say F(), the output of which dictates my logic. So I need the result of it to proceed further in my code.

The problem is F() does some I/O and so spawns off a thread to do the functionality. They gave me a few callbacks to implement which are called to return the status/values from the thread. This is fine except that the function F() returns immediately and the rest of my code keeps running. I don’t want that as I have to allocate some objects based on the outcome of F() and so I really want to wait for F() to really finish.

This should be a problem people run into quite often. What is the correct way to deal with this? Is it a problem in my design or the third party library? Or did I not understand the library very well?

For the sake of completeness I am working on an android app and the library is the facebook sdk. The function is the single sign-on function authorize(). Though this is a generic enough problem that I thought is best addressed in the Java forum.

Thanks for any help.
– P

  • 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-21T17:43:03+00:00Added an answer on May 21, 2026 at 5:43 pm

    Think about this:
    The main thread of an android app is the thread that manages the UI, so if you want to wait until the method f() finishes, the screen will freeze. You probably will think that a good solution is to spawn a new thread and call the method f() inside it and use a callable or future to wait f(). Wrong. The code below the thread creation will execute so you will have the same problem.
    Then how can you solve this? Simple. Redesign your application and use a listener, a.k.a. callback.

    EDIT: A listener or callback is just a method (or methods) that a class A implement and passes this implementation to a class B to execute it. Class B doesn’t know what he is executing, he just executes a method.

    Example:

    public class A{
        public A (){
            B b = new B (new B.Listener (){
                @Override
                void onResult (String result){
                    //result is "Core dumped!"
                }
            });
            b.aMethod ();
        }
    }
    
    public class B{
        private final Listener listener;
    
        public static interface Listener{
            void onResult (String result);
        }
    
        public B (Listener listener){
            if (listener == null) throw new IllegalArgumentException ("The \"listener\" parameter cannot be null.");
            this.listener = listener;
        }
    
        public void aMethod (){
            //Do something interesant
            //...
            String error = "Core dumped!"
    
            //Call the listener
            listener.onResult (error);
        }
    }
    

    In your case you will have something similar to this:

    public class YourClass{
        public void aMethod (){
            FaceBookClass fb = new FaceBookClass ();
            fb.setListenerToF (new FaceBookClass.FListener (){
                @Override
                void theCallback (Result aResult){
                    //Do something with result
                }
            });
            fb.f ();
    
        }
    }
    
    public class FaceBookClass{
        public static interface FListener{
            void theCallback (Result aResult);
        }
    
        public void f (){
            ...
        }
    
        public void setListenerToF (FListener listener){
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie here...can I write one program which incorporates .NET LINQ and also various Java
Java Newbie here. I have a JFrame that I added to my netbeans project,
Java newbie here, I'm having trouble setting a new line in this code: String
newbie here. I have a java application and I am working on to monitor
Java, ANTLR and Netbeans newbie here. I have installed a jdk and netbeans. I
Hey, guys. Newbie to tomcat/apache, java returner here. I'm trying to run th step-by-step
Java has the thread dump which is triggered by a signal 3 sent to
Java newbie. I am trying to run a java main class from cmd line
I'm a newbie on Java EE and got a problem that I don't understand
I am a newbie to Java Web Application development. So far all 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.