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

xulihang

Active Member
Licensed User
Longtime 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:

B4X:
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
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
I didn't find a good solution by reading these tutorials.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime 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
B4X:
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
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime 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.

B4X:
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 map) as ResumableSub
    'S3 requires some time to process and will block the thread.
    return  value
End Sub
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime 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.
B4X:
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:
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
I need A1 to end after A3 ends, like this.

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

B4X:
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
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime 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:
Upvote 0

xulihang

Active Member
Licensed User
Longtime 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.
 

Attachments

  • resumables_test.zip
    294.7 KB · Views: 166
Last edited:
Upvote 0
Top