Android Question How do you write multi-threaded applications?

John H. Guillory

Member
Licensed User
Longtime User
I got a feeling I may know the answer. I have a feeling it's one of two things, but reading an article about making the main activity as small as possible and using multi-threaded applications to not bog down the application leads me to ask just in case. From what I read, it appears that Android supports multi-threaded applications running, so suppose I have an application with Thread1 and Thread2, and a subroutine called Job1 that calls both, how do I code it such that Thread1 and Thread2 run at the same time, and how would I check to see when they have finished? I've got a sneaky feeling that I need to create a service for this.
 

John H. Guillory

Member
Licensed User
Longtime User
Just when drawing a bunch of lines, and some other task, the phone got bogged down, so I thought I could speed it up if I had it draw part in one thread, then part in another thread. It sounds like what I might have to do is build a library in Java, and write the threads in java and call the threads from a java class....
 
Upvote 0

John H. Guillory

Member
Licensed User
Longtime User
I found an article on mulithreaded apps, and downloaded the library. I used it on a few routines. Strangely enough the one that bogged down the most was when I convert a classic Basic routine to display a bunch of random circles on the screen... Eg. I have 2 threads each basically like this:

Sub C1a
Dim X,Y, r, T,C1 As Int

For T = 1 To 500
X = Rnd(0,Panel1.Width)
Y = Rnd(0,Panel1.Height)
r = Rnd(0,Panel1.Width / 2)
C1 = Colors.ARGB(Rnd(50,255),Rnd(0,255),Rnd(0,255),Rnd(0,255))
c.drawcircle(X,Y,r,C1,True,1.0)
Next
End Sub

The other thread is Sub C2a ... That's the one that seems to bog it down the most. I originally was trying to do 32000 circles, I broke it down into 2 threads each doing 16000 random circles. I tried using doevents and sleep, I tried doing an Activity.Invalidate to have it update the screen... Either way, with a loop of at least 1000 or so, I get the phone kinda bogged down for a few seconds not showing anything till it's completely done drawing, then it shows the menu bar and the circles... On the multi-threaded, the second thread finishes a few seconds later and more circles appear.

Then I do a moire pattern, and a circle pattern, and generate random boxes (each on their own activity). The moire pattern shows instantly, no problems with it:

Sub M1a
For T = 0 To X Step 2
c.DrawLine(T,0,X-T,Y,Colors.Cyan,1.0)
Next
End Sub

Sub M2a
For T = 0 To Y Step 2
c.DrawLine(0,T,X,Y-T,Colors.Cyan,1.0)
Next
End Sub

M1a and M2a are 2 threads for the demo. Each thread runs at the same time, so it draws the Horizontal and Vertical parts at the same time. Very fast too!

Sub chaser
Dim T As Int
x = 0
y = 0
For T = 1 To 20000
Lx = x
Ly = y
x = Rnd(0,Panel1.Width)
y = Rnd(0,Panel1.Height)
c.DrawLine(x,y,Lx,Ly,Colors.ARGB(255,0,T,0),5)
Next
End Sub

On Chaser, I tried to use a delay and erase the previous line. I'd love to have it draw the line, delay, erase, re-draw a new line. But using the delay command someone else posted, it either delays too quickly, or delays far too long. I either get no line (erasing it instantaneous) or I get draw a line, pause 3-4 minutes, draw another line... So I went with this... Instantaneous... And yup, I tried invalidate on that too....
 
Upvote 0
Top