CountDownLatch in Android locking thread - java

I've just started playing around with CountDownLatch in my Android app. Currently I am trying to make two Volley requests to my api, and wait until the data has been retrieved and stored before continuing with thread execution.
This is a sample of my code:
// new CountDownLatch for 2 requests
final CountDownLatch allDoneSignal = new CountDownLatch(2);
transactions.getResourcesForRealm(Contact.class, "", new ICallBack<Contact>() {
#Override
public void onSuccess(ArrayList<Contact> resources, String resourceId) {
transactions.createRealmObject(resources, Contact.class);
allDoneSignal.countDown();
}
#Override
public void onFail(ArrayList<Contact> resources) {
}
});
transactions.getResourcesForRealm(Meeting.class, "", new ICallBack<Meeting>() {
#Override
public void onSuccess(ArrayList<Meeting> resources, String resourceId) {
transactions.createRealmObject(resources, Meeting.class);
allDoneSignal.countDown();
}
#Override
public void onFail(ArrayList<Meeting> resources) {
}
});
try {
allDoneSignal.await();
// continue executing code
// ...
} catch (InterruptedException e) {
e.printStackTrace();
}
The issue is that it doesn't seem to "complete" the countdown and therefore freezes because the latch is never released. I have confirmed that the API requests are working and the onSuccess callback is hit successfully, but the thread hangs.
UPDATE
I've just noticed that with the CountDownLatch set to 0, it hits onSuccess, but when I set it to anything greater than 0, it freezes and onSuccess is never called. Seems like something's funky with the threading.

Your code is too error prone, you need to call countDown() in a finally block and call it also in onFail otherwise in case of failure your application will freeze for ever. So your code should rather be something like:
transactions.getResourcesForRealm(Contact.class, "", new ICallBack<Contact>() {
#Override
public void onSuccess(ArrayList<Contact> resources, String resourceId) {
try {
transactions.createRealmObject(resources, Contact.class);
} finally {
allDoneSignal.countDown();
}
}
#Override
public void onFail(ArrayList<Contact> resources) {
allDoneSignal.countDown();
}
});
transactions.getResourcesForRealm(Meeting.class, "", new ICallBack<Meeting>() {
#Override
public void onSuccess(ArrayList<Meeting> resources, String resourceId) {
try {
transactions.createRealmObject(resources, Meeting.class);
} finally {
allDoneSignal.countDown();
}
}
#Override
public void onFail(ArrayList<Meeting> resources) {
allDoneSignal.countDown();
}
});

Sorry for the late answer, but if it's still any help to anyone:
You need to do the ".await" in a separate thread because it blocks the current thread.
Example:
final Handler mainThreadHandler = new Handler(Looper.getMainLooper());
new Thread(new Runnable() {
#Override
public void run() {
allDoneSignal.await();
mainThreadHandler.post(new Runnable() {
doSomethingWhenAllDone();
});
}
}).start()

Related

React Native Android Bridge Error: Must be called on main thread

I am using the Sumup SDK to create a bridge to React Native. Most of the hard work is done but I am trying to call a specific function to wake up the card reader before a transaction is processed.
The original code I had was this:
#ReactMethod
public void prepareCardTerminal() {
SumUpAPI.prepareForCheckout();
}
}
The RN bridge then calls this function like this:
static prepareCardTerminal() {
NativeRNSumup.prepareCardTerminal();
}
How ever this gives me a React Native error of:
Must be called on main thread
I read that this could mean it needs to be run on the UI thread so I rewrote the function to be:
#ReactMethod
public void prepareCardTerminal() {
new Thread(new Runnable() {
public void run() {
SumUpAPI.prepareForCheckout();
}
});
}
However this doesn't have the intended results (even though it doesn't show any errors).
Any tips would be much appreciated.
Edit: I found a solution to this issue. I used UiThreadUtil:
import com.facebook.react.bridge.UiThreadUtil;
...
#ReactMethod
public void prepareCardTerminal() {
UiThreadUtil.runOnUiThread(new Runnable() {
#Override
public void run() {
SumUpAPI.prepareForCheckout();
}
});
}
You can do something like this:
#ReactMethod
public void prepareCardTerminal() {
// Get a handler that can be used to post to the main thread
Handler mainHandler = new Handler(context.getMainLooper());
Runnable myRunnable = new Runnable() {
#Override
public void run() {
SumUpAPI.prepareForCheckout();
}
};
mainHandler.post(myRunnable);
}
Or even simpler:
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
SumUpAPI.prepareForCheckout();
}
});
The answer posted as an edit to the question is correct. The answer by #waquar-ulhaq is technically correct but using UiThreadUtil is way simpler and internally does use a Handler
import com.facebook.react.bridge.UiThreadUtil;
...
#ReactMethod
public void prepareCardTerminal() {
UiThreadUtil.runOnUiThread(new Runnable() {
#Override
public void run() {
SumUpAPI.prepareForCheckout();
}
});
}

How to triggerShutdown a Guava AbstractScheduledService?

I'm using a few services inheriting from the AbstractScheduledService, which get managed by a ServiceManager. Everything works fine, but now, there's a service whose runOneIteration takes a rather long time, and as the result, my process takes too long to terminate (more than five seconds).
There are other services inheriting from AbstractExecutionThreadService, which had a similar problem, which I could solve via
#Override
protected final void triggerShutdown() {
if (thread != null) thread.interrupt();
}
and storing private volatile thread in the run method. However, there's no triggerShutdown for AbstractScheduledService as stated in this issue.
I already considered alternatives like making runOneIteration do less work, but it's both ugly and inefficient.
I can't override stopAsync as it's final and I can't see anything else. Is there a hook for doing something like this?
Can you work with this? Was there any reason you couldn't add a triggerShutdown yourself?
class GuavaServer {
public static void main(String[] args) throws InterruptedException {
GuavaServer gs = new GuavaServer();
Set<ForceStoppableScheduledService> services = new HashSet<>();
ForceStoppableScheduledService ts = gs.new ForceStoppableScheduledService();
services.add(ts);
ServiceManager manager = new ServiceManager(services);
manager.addListener(new Listener() {
public void stopped() {
System.out.println("Stopped");
}
public void healthy() {
System.out.println("Health");
}
public void failure(Service service) {
System.out.println("Failure");
System.exit(1);
}
}, MoreExecutors.directExecutor());
manager.startAsync(); // start all the services asynchronously
Thread.sleep(3000);
manager.stopAsync();
//maybe make a manager.StopNOW()?
for (ForceStoppableScheduledService service : services) {
service.triggerShutdown();
}
}
public class ForceStoppableScheduledService extends AbstractScheduledService {
Thread thread;
#Override
protected void runOneIteration() throws Exception {
thread = Thread.currentThread();
try {
System.out.println("Working");
Thread.sleep(10000);
} catch (InterruptedException e) {// can your long process throw InterruptedException?
System.out.println("Thread was interrupted, Failed to complete operation");
} finally {
thread = null;
}
System.out.println("Done");
}
#Override
protected Scheduler scheduler() {
return Scheduler.newFixedRateSchedule(0, 1, TimeUnit.SECONDS);
}
protected void triggerShutdown() {
if (thread != null) thread.interrupt();
}
}
}

Android - Run response call in main thread

I want to make downloader, which download data and then call function in UI thread. I have this in main activity
onCreate(){
...
dataRepository.downloadIfNewOrEmpty(new DownloadResponse() {
#Override
public void SuccessResponse(Response response) {
// do something in UI
}
});
}
My function downloadIfNewOrEmpty looks for now only simple with sleep()
public void downloadIfNewOrEmpty(final DownloadResponse response){
//final Handler handler = new Handler();
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
response.SuccessResponse(ResponseCode.SUCCESS);
/*handler.post(new Runnable() {
#Override
public void run() {
response.SuccessResponse(ResponseCode.SUCCESS);
}
});*/
}catch (Exception e){
// Log...
}
}
}).start();
}
If I run this code, it normally does the job and update my UI. I found this solution with Handler (android.os.Handler) but if I run it without or with Handler (commented version) it works same.
Although without handler function SuccessResponse is run in UI thread?
Thank you
Although without handler function SuccessResponse is run in UI thread?
Yes, because response is instance of DownloadResponse which is passed from UI Thread as parameter to downloadIfNewOrEmpty.

Avoid using CountDownLatch to wait for many threads before sending the callback

So I have a list of Track Ids that for each track Id I need to execute a network request to get the track details, I am using a for loop to launch all the requests and a latch to wait for all the requests to be completed. After they are completed then the callback is sent with the List of Tracks that have already populated.
I would like to know if there is any better way to do this, maybe with RxJava ?
I am using Retrofit 2.0 in Android.
public IBaseRequest batchTracksById(final TrackIdList trackIdListPayload, final IRequestListener<TracksList> listener) {
final TracksList tracks = new TracksList();
final Track[] trackArray = newrack[trackIdListPayload.getTrackIds().length];
tracks.setTrack(trackArray);
final CountDownLatch latch = new CountDownLatch(trackArray.length);
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
latch.await();
handler.post(new Runnable() {
#Override
public void run() {
listener.onRequestUpdate(null, tracks, null, true);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
for (String id : trackIdListPayload.getTrackIds()) {
getTrackById(id, new IRequestListener<Track>() {
#Override
public void onRequestFailure(IBaseRequest request, Exception exception) {
latch.countDown();
}
#Override
public void onRequestUpdate(IBaseRequest request, Track track, RequestState state, boolean requestComplete) {
//iterate through the tracks and update the ones in the thing
int i = 0;
for (String s : trackIdListPayload.getTrackIds()) {
if (s.equals(track.getTrackId())) {
trackArray[i] = track;
// don't break here, as we may have a case where we have multiple instances of the same trackId (although
// at the moment a request will be made for each anyway...
}
i++;
}
latch.countDown();
}
});
}
return null;
}
If you want to make all the requests asynchronously and wait for them to return you can do this (lambdas for brevity and readability):
tracks.flatMap(track -> getTrackDetails(track.id)
.subscribeOn(Schedulers.io()))
.toList()
.doOnNext(list -> processTrackList())
...
If you require that the results are returned in the order of tracks but are still requested asynchronously then in soon to be released rxjava 1.0.15 you will be able to do this
tracks.concatMapEager(track -> getTrackDetails(track.id)
.subscribeOn(Schedulers.io())
.toList()
.doOnNext(list -> processTrackList())
...
If I understand correctly, you have a list of tracks as input and you want a list of webservice results. Here's a simple way to do that with RxJava if you can make your network call synchronous (rxjava will handle the background processing for you).
Observable.from(trackList)
.map(new Func1<Track, Response>() {
#Override
public Response call(Track track) {
return makeRequestSynchronously(track.id());
}
})
.toList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<Response>>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(List<Response> responses) {
}
});
Edit: You can change Retrofit to return observables from webservice, if you do that you will need to change map to the following
.flatMap(new Func1<Track, Observable<Response>>() {
#Override
public Observable<Response> call(Track track) {
return makeRequestThatReturnsObservable(track.id());
}
})

Is this a convenient way of running code in a background thread and return a result via a callback executed in the calling thread?

In the last days I have found myself using this approach for asynchronously performing some long operation (several seconds), and then return some value via a callback that must execute on the caller thread, which is typically but not necessarily the UI thread.
public abstract class DoSomethingCallback
{
public abstract void done(Object result);
}
public void doSomething(final Object param, final DoSomethingCallback doSomethingCallback)
{
// Instantiate a handler for the calling thread
final Handler handler = new Handler();
// Start running the long operation in another thread
new Thread(new Runnable() {
#Override
public void run() {
// Do a long operation using "param" as input...
Object result = longOperation(param);
// Return result via a callback, which will run in the caller thread
handler.post(new Runnable() {
#Override
public void run() {
doSomethingCallback.done(clearBytes);
}
});
}
}).start();
}
This seems to work pretty well and is very simple to use. However, I somehow suspect it might have some problems I'm not aware of. So the question is, what are the potential issues of this approach? What are better alternatives than manually creating and running a thread? I'm seeking for simplicity and robustness.
The only problem is that such approach breaks encapsulation: the second thread not only computes the result, but also dictates what the caller thread should do with it. So I'd better refactor your code as follows:
public abstract class DoSomethingCallback {
final Handler handler = new Handler();
public void post(final Object result) {
handler.post(new Runnable() {
#Override
public void run() {
doSomethingCallback.done(result);
}
});
}
public abstract void done(Object result);
}
public void doSomething(final Object param, final DoSomethingCallback doSomethingCallback) {
// Instantiate a handler for the calling thread
final DoSomethingCallback handler = new DoSomethingCallback () {
void done(Object result) {
...
}
};
// Start running the long operation in another thread
new Thread(new Runnable() {
#Override
public void run() {
// Do a long operation using "param" as input...
Object result = longOperation(param);
// Return result via a callback, which will run in the caller thread
handler.post(result);
});
}
}).start();
}

Categories