B4J Question Run processes simultaneously

jroriz

Active Member
Licensed User
Longtime User
Hi.
I would like the Sub LongProcess to be executed SIMULTANEOUSLY, ie the 3 processes beginning and ending at the same time.
As it is in the code, they start together, but the second only ends after the end of the first, and the third after the end of the second.
Can anyone help?

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    
    LongProcess(0)
    LongProcess(1)
    LongProcess(2)

End Sub

Sub LongProcess(ix As Int)
    Log("start time: " & ix & " " & DateTime.Time(DateTime.Now))
    Sleep(0)
    For x = 1 To 300000000
        
    Next
    Log("end time: " & ix & " " & DateTime.Time(DateTime.Now))
End Sub

log:
Waiting for debugger to connect...
Program started.
start time: 0 12:13:41
start time: 1 12:13:41
start time: 2 12:13:41
end time: 0 12:13:42
end time: 1 12:13:44
end time: 2 12:13:46
 

DonManfred

Expert
Licensed User
Longtime User
Watch the resumeable subs tutorial and get familar with resumeable subs.
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
Thanks for your attention. This was the best I got after following the tutorial. I made several attempts but I did not succeed.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
In a B4J program all your code runs on the single main thread so you cannot achieve true simultaneous execution. Even using Resumable Subs you will still get consecutive execution of your Subs. You can achieve a measure of simultaneity by running each Sub on a separate thread, each of which might execute a different core of your CPU or might be time sliced with other threads on the same CPU core. What actually happens is outside your control.

Even if you use threads although the processes may start at the same time they will never all end at same time owing to uncertainties in the execution process.

You don't say why you want to simultaneous execution but I doubt that it is really necessary. If you want to try my B4A Threading library https://www.b4x.com/android/forum/threads/threading-library.6775/ should work in B4J in release mode.
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
In a B4J program all your code runs on the single main thread so you cannot achieve true simultaneous execution. Even using Resumable Subs you will still get consecutive execution of your Subs. You can achieve a measure of simultaneity by running each Sub on a separate thread, each of which might execute a different core of your CPU or might be time sliced with other threads on the same CPU core. What actually happens is outside your control.

Even if you use threads although the processes may start at the same time they will never all end at same time owing to uncertainties in the execution process.

You don't say why you want to simultaneous execution but I doubt that it is really necessary. If you want to try my B4A Threading library https://www.b4x.com/android/forum/threads/threading-library.6775/ should work in B4J in release mode.
I need to show 4 different information on the screen, starting from a common information.
The problem is that each of these information takes about 4 seconds to complete, and one does not depend on the other.
My wish is to finish the process in 4 seconds, doing the four processes together.
I do not know if I could make myself clear.
I will try your lib.
Thanks.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
each of these information takes about 4 seconds to complete
nonstop or is there a delay as example wainting for a http response?

its more like this, as soon you use sleep the sub interrupts and other continue until the sleep there.
B4X:
Sub LongProcess(ix As Int)
    Log("start time: " & ix & " " & DateTime.Time(DateTime.Now))   
    For x = 1 To 100000
        Sleep(0)
    Next
    Log("end time: " & ix & " " & DateTime.Time(DateTime.Now))
End Sub
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
In fact it is a mathematical process that has 5000 interactions.
then i think the resumeable subs make no sense for you.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should do it like this:
B4X:
Sub LongProcess(ix As Int)
    Log("start time: " & ix & " " & DateTime.Time(DateTime.Now))

    For x = 1 To 300000000
        if x Mod 1000000 = 0 Then Sleep(0) 'try different values
    Next
    Log("end time: " & ix & " " & DateTime.Time(DateTime.Now))
End Sub
This will allow you four processes to run together (on the same thread, but still together).
 
Upvote 0
Top