Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Hint: "It's not working" is going to get you downvotes, and rightfully so. Be specific in what exactly you have observed.
-
@yellow-dog That still sucks because the thread has to resume periodically just to poll the variable and check whether it should resume. That adds either noticeable delay or unnecessary load, depending on whether the sleep period is chosen long or short. Both are especially bad on mobile devices.
Dunno how Java implements that, but with Posix threads, a pthread condition could be used, and with WinAPI, WaitForSingleObject() on an event is an option. -
@Fast-Nop java threads are usually in an infinite loop anyway, checking a variable aint gonna impact them
-
@yellow-dog Threads in general are in an infinite loop unless they're short-lived worker threads. But event driven programming is the best method for dealing with, well, events.
-
@Fast-Nop yeah, i dont think using a thread is the right call either, but adding rxjava just for this is overkill.
Maybe use a Timer object? That should have a pause. -
@yellow-dog True, for this specific case, a timer would be even better, unless this is just an example and the actual job is to do something else.
-
twped225yTake a look here and implementation of a ManualResetEvent.
https://stackoverflow.com/questions...
In a thread you can let the thread execution wait on the event which is triggered elsewhere.
It's safe and does not take any resources waiting for the event to happen.
Once the event is triggered you must reset it again so it's ready for the next trigger. -
Okay, a lot of bigwords here that i don't know... Volatile variable, timer, rx, and one even .net paradigm implementation.
Let me check if i can find some mvp on these topics. But thanks for help, @yellow-dog @Fast-Nop , @twped ! -
@StopWastingTime You are missing a tag Android here. It's a pain to copy-paste and mock every class you have either misspelled or iported from Android libs.
THe comment under your SO post is correct. Go easy. First try to make a JUnit test that works w/o all those android Views and whatnot.
When it works in JUnit -- port it to Android. If it breaks there then you know what to blame and what docs to read. -
@twped this is no place for beggars. Anyone gives ++s at their own will. Asking for ++ here will most likely earn you a shitstorm of --s. This community tollerates but beggars and spammers
-
@netikras thanks for your advice about junit. I actually do that for most of business logics, just hesistated working with threads there because i might have read somewhere that google has modified the source code of java threads in Android (and only the java packages work in the junit, right?)
Also your second approach worked like a charm. Its just that now i am realising that my goal of making an independent thread class is not possible, the one that would handle all the play/pause/stop/start logics via event callbacks or something.
I mean, the locks are inherently being handled by the userAction, its not like user passed a set of codelines and my pausabile thread knows when to run them/pause running them automatically. -
@netikras
I think thread should rather have its function named 'execute' instead of 'start'. 'start' gives a feeling that if a thread can be 'started', then it could be stopped or temporary stopped(aka paused) and resumed.
But rather its just a bunch of lines given to a side processor. For executing where the calling thread would have no role in its execution unless used with some hacky code, right? -
@twped try posting some oc code memes, your score will sore. And i would die for that green bubble. Once you you loose it, its gone forever
-
@StopWastingTime
Look up runnable and completable future
https://baeldung.com/java-completab...
You're better off letting a scheduler handle operations than maintaining long running threads waiting to do work.
Related Rants
People experienced with java/ multi threading , can you tell me how you build a pausing mechanism for threads? Like, if my bg thread is supposed to count for 100 seconds, and i need a mechanism to :
A: start it,
B: stop it on its counting completion/ manually
C: pause its execution at a number on a button click
D: resume its counting from the same number on another button click
How would i do that?
SO Question link here: https://stackoverflow.com/questions...
question
java