How to retrieve mesosfer data using java multithreading in android studio

Home - How to retrieve mesosfer data using java multithreading in android studio

Iman Iman asked 3 years ago

I tried to create a new thread that is updated every second to retrieve data from mesosfer. And the problem occurs when it’s trying to execute the asynchronous function “MesosferData.findAsync()”. Somehow the function is not executed. I think the function will be executed only in main thread. What should I do?

1 Answers
Alann Maulana Staff answered 3 years ago

Hi Iman,
There is another way to convert asynchronous function to synchronous, so you can execute it for any other thread. Here is some example :

public class AsyncToSync {
    private static final Object LOCK = new Object();
    private boolean finish = false;
    private MesosferException exception;
    private List<MesosferData> objects;

    public static List findBeaconSync() throws InterruptedException, MesosferException {
        MesosferData.getQuery("Beacon").findAsync(new FindCallback<MesosferData>() {
            public void done(List<MesosferData> objects, MesosferException e) {
                AsyncToSync.this.exception = e;
                AsyncToSync.this.objects = objects;

                synchronized (LOCK) {
                    AsyncToSync.this.finish = true;

        synchronized (LOCK) {
            while (!finish) {

        if (exception != null) {
            throw exception;

        return objects;

Then you can find Beacon data synchronous by calling :

try {
List<MesosferData> objects = AsyncToSync.findBeaconSync();
} catch (InterruptedException ie) {
// another thread interrupting
} catch (MesosferException me) {
// error from Mesosfer SDK/Server


Your Answer