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...

  • 1
    Give it a volatile variable, pause execution depending its value.
  • 3
    Hint: "It's not working" is going to get you downvotes, and rightfully so. Be specific in what exactly you have observed.
  • 1
    @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.
  • 1
    @Fast-Nop java threads are usually in an infinite loop anyway, checking a variable aint gonna impact them
  • 1
    @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.
  • 3
    @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.
  • 3
    @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.
  • 1
    Take a look here and implementation of a ManualResetEvent.


    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.
  • 1
    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 !
  • 1
    @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.
  • 1
    I've answered your question
  • -1
    @StopWastingTime ++ for my answer would be nice
  • 3
    @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
  • 1
    @netikras yeah thanks for the info and I get the point. Being a bit green behind my ears - just joined today you see. But anyway who gives a shit.
  • 1
    @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.
  • 1
    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?
  • 0
    @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
  • 0
    @StopWastingTime oh noooo the green bubble is gone!!
  • 1
    The best reasons to add rxjava:

    - You are writing java
  • 1
    @SortOfTested Best part of the Android toolchain: the NDK. ^^
  • 0
    Look up runnable and completable future


    You're better off letting a scheduler handle operations than maintaining long running threads waiting to do work.
Add Comment