#Is it possible pause my program by thread for 5 seconds to call an endpoint

19 messages · Page 1 of 1 (latest)

last quiver
#

And after 5 seconds, rest of the code will run.
I did below code but somehow it is not working:

final String[] quoteData = new String[1];
new java.util.Timer().schedule(
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                        // your code here
                        quoteData[0] = getQuote();
                        Log.e("DAI", quoteData[0]);
                    }
                },
                5000
        );

Any thoughts?
And one more thing, here also see I just need String variable but for the Thread it forces me to make the String variable final and array too 😭
Thanks!

uncut crescentBOT
#

This post has been reserved for your question.

Hey @last quiver! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

glad valley
#

Use ScheduledExecutorService

leaden crow
glad valley
#

You could achieve what you want

#

You said "not working"

#

If you want a paused threading thingy going, that's the service

leaden crow
#

I did not say that, OP did. And the same thing with a ScheduledExecutorService would be equally not working

glad valley
#

oh

leaden crow
#

TimerTask works, you know

glad valley
#

I thought you were OP

glad valley
leaden crow
#

Since the beginning actually. ExecutorServices became handy when scheduling things become a more-than-one-time occurrence

glad valley
#

Perhaps they could simply make a Future and return the .get() after the 5 seconds

#

wouldn't that work?

leaden crow
#

Fundamentally we need to know why it doesn't work. Which comes with how do they know that it doesn't work, what happens instead

#

I suspect they're doing android code and trying to wait on worker thread

mighty merlin
#

What's the deal with the 5 seconds? What's the intended goal? Waiting around for anything sounds like a terrible idea. I'm with @glad valley on this one, completableFuture