1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

B4J Question How to use resumable subs to prevent blocking the thread when many subs are stringed

Discussion in 'B4J Questions' started by xulihang, Jun 1, 2019.

  1. xulihang

    xulihang Active Member Licensed User

    Hi there,

    I find it puzzling to use sleep when I have one sub calling a second sub which calls the third sub.

    Like this:

    Code:
    Sub S1
        
    'update UI
        S2
        
    'update UI
    End Sub

    Sub S2
        
    'do something
        S3(param)
    End Sub

    Sub S3(param as map)
        
    'S3 requires some time to process and will block the thread.
    End Sub
    Where to put sleep to prevent blocking the thread?

    Thanks
     
  2. DonManfred

    DonManfred Expert Licensed User

  3. xulihang

    xulihang Active Member Licensed User

    I didn't find a good solution by reading these tutorials.
     
  4. MarkusR

    MarkusR Well-Known Member Licensed User

    in every sub that need a long time you can use sleep but that means the sub above will continue, so you need wait for to get a sequence.

    pseudo-code
    Code:
    Sub S2
        
    'do something
        wait for S3(param)
    End Sub

    Sub S3(param as map)
        
    'S3 requires some time to process and will block the thread.

       sleep 
       sleep
       sleep

    End Sub
     
  5. xulihang

    xulihang Active Member Licensed User

    Thanks, Markus. So if these subs all need to return a value, then I have to use "wait for" for all of them? This is not that convenient though.

    And I find that for computing-intensive tasks, the UI will still freeze though sleep is used. jShell runs a different process and using wait for with it is okay.

    Code:
    Sub S1
        
    'update UI
        wait fo (S2) Complete (result as Object)
        
    'update UI
    End Sub

    Sub S2 as ResumableSub
        
    'do something
        wait for (S3(param)) Complete (result as Object)
        
    return value
    End Sub

    Sub S3(param as mapas ResumableSub
        
    'S3 requires some time to process and will block the thread.
        return  value
    End Sub
     
    Last edited: Jun 2, 2019
  6. MarkusR

    MarkusR Well-Known Member Licensed User

    wait for is used if u need the return value there for a condition.
    if you use sleep the events will also be processed. its like a heartbeat if the processing stuck in a sub.
    you can store calculated data also into a type or a class and process this later.

    this way is also valid but you not have a sequence.
    Code:
    Sub A1
       
        
    Log("A1 Start")
       
        A2

        
    Log("A1 End")
       
    End Sub

    Sub A2

        
    Log("A2 Start")
       
        A3
       
        
    Log("A2 End")

    End Sub

    Sub A3()

        
    Log("A3 Start")

        
    Log("start time: " & DateTime.Time(DateTime.Now))
        
    Dim x As Long
        
    For x = 1 To 100000000
            
    If x Mod 1000 = 0 Then Sleep(0'try different values
        Next
        
    Log("end time: " & DateTime.Time(DateTime.Now))
       
        
    Log("A3 End")

    End Sub
    https://www.b4x.com/android/forum/threads/run-processes-simultaneously.106208/#content
     
    Last edited: Jun 2, 2019
    xulihang likes this.
  7. xulihang

    xulihang Active Member Licensed User

    I need A1 to end after A3 ends, like this.

    Code:
    Waiting for debugger to connect...
    Program started.
    A1 Start
    A2 Start
    A3 Start
    start time: 
    15:59:06
    end time: 15:59:17
    A3 
    End
    A2 
    End
    A1 
    End
    So wait for is needed. I think I have to use "wait for" for several times.

    Code:
    Sub A1
        
    Log("A1 Start")
        
    wait for (A2) complete (result As Object)
        
    Log("A1 End")
    End Sub

    Sub A2 As ResumableSub
        
    Log("A2 Start")  
        
    wait for (A3) complete (result As Object)  
        
    Log("A2 End")
    End Sub

    Sub A3() As ResumableSub
        
    Log("A3 Start")
        
    Log("start time: " & DateTime.Time(DateTime.Now))
        
    Dim x As Long
        
    For x = 1 To 100000000
            
    If x Mod 1000 = 0 Then Sleep(0'try different values
        Next
        
    Log("end time: " & DateTime.Time(DateTime.Now))  
        
    Log("A3 End")
    End Sub
     
  8. xulihang

    xulihang Active Member Licensed User

    I find that in order to use resumable subs instead of another thread to avoid blocking the ui, I have to find which sub (I have too many subs) takes time and make it a resumable sub. In this way, the UI is not blocked.

    As for methods wrapped by JavaObject(see: How to avoid javaobject method blocking the ui thread), it cannot be divided into small subs any more and I think resumable subs does not help much and using another thread may be a better way.
     
    Last edited: Jun 2, 2019
    MarkusR likes this.
  9. xulihang

    xulihang Active Member Licensed User

    This is the project which I have used to test.

    This program reads a txt file, segments the text into sentences and saves it to a json file.
     

    Attached Files:

    Last edited: Jun 2, 2019
    MarkusR likes this.
  10. Erel

    Erel Administrator Staff Member Licensed User

    Your code runs fine. The UI is responsive. I took a quick look and I'm pretty sure that you can optimize it.

    For example you can use B4XSet from B4XCollections to avoid adding duplicates in a much more efficient way (breakPositions should be a B4XSet).
     
    xulihang likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice