So here's some code on http://groups.google.com/group/android-developers/browse_thread/thread/16a2216b39513674 in which the poster writes that he's shortened ideas into a single threaded app. A respondent below his post says, essentially, that's great, nice work, but there are benefits to multithreading so things don't interfere with other stuff on your handset.
My question is: Evidently, the original poster reduced, distilled ideas from multithreaded examples he saw in order to make them easier to read, posted them, cool. But what would a multithreaded version of the below code look like?
Would Runnable be on a separate thread? Or the whole callback? Or not those but something else? Anything else?
Would it be even better if it were run as a service, and then a Runnable thread spawned from the service? Is a service considered a separate thread from the UI?
As you can tell this question is about the basics of multithreading for Android. Thanks for any info.
public class Something extends Activity {
@Override
protected void onCreate(Bundle saved) {
super.onResume();
final EGL10 egl = (EGL10) EGLContext.getEGL();
final EGLDisplay eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
int[] version = new int[2];
egl.eglInitialize(eglDisplay, version);
int[] configSpec = {
// EGL10.EGL_DEPTH_SIZE, 8,
EGL10.EGL_NONE
};
final EGLConfig[] config = new EGLConfig[1];
int num_configs[] = new int[1];
egl.eglChooseConfig(eglDisplay, configSpec, config, 1, num_configs);
final EGLContext eglContext = egl.eglCreateContext(eglDisplay, config[0], EGL10.EGL_NO_CONTEXT, null);
// Setting up layouts and views
SurfaceView view = new SurfaceView(this);
setContentView(view);
SurfaceHolder holder = view.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
final GL10 gl = (GL10) eglContext.getGL();
final Handler handler = new Handler();
holder.addCallback(new Callback() {
private EGLSurface surface;
private Runnable painter;
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// initialize GL projection and other stuff
// gl.glMatrixMode(GL10.GL_PROJECTION);
// gl.glFrustumf(left, right, bottom, top, zNear, zFar);
// ...
painter = new Runnable() {
@Override
public void run() {
drawFrame(gl);
egl.eglSwapBuffers(eglDisplay, surface);
handler.post(this);
}
};
handler.post(painter);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
surface = egl.eglCreateWindowSurface(eglDisplay, config[0], holder, null);
egl.eglMakeCurrent(eglDisplay, surface, surface, eglContext);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
handler.removeCallbacks(painter);
}
});
}
private void drawFrame(GL10 gl) {
// Frame drawing...
long t = System.currentTimeMillis() % 10000;
gl.glClearColor(t / (float) 10000, t / (float) 10000 ,1 , 1);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
}