I have a question related to the following link: What's the difference between Thread start() and Runnable run()
In this question, I see a person creating runnable objects and then initializing them in two different ways. So, does this mean that you could pass these runnables around to other things at run time?
I want to pass code to a preexisting thread to be executed within that thread's loop. I was looking around and from what I can tell, you would want to create a dedicated runnable class like the following:
public class codetobesent implements Runnable
{
public void run()
{
..morecodehere.
}
...insertcodestuffhere
}
But how would I pass this to a thread that is already running? Say I'm trying to make a game and I have something special I want the renderer to do in its thread. How would I pass this runnable to that thread and have it run this data correctly?
My current implementation of my rendering thread is the following, I pulled it off of a tutorial site, and it has worked pretty well so far. But I want to know how to pass things to it so I can run more than what's just in the preset loop.
class RenderThread extends Thread
{
private SurfaceHolder _curholder;
private UserView curview;
private boolean runrender = false;
public RenderThread (SurfaceHolder holder, UserView thisview)
{ //Constructor function - This gets called when you create a new instance of this object.
curview = thisview;
_curholder = holder;
}
public SurfaceHolder getThreadHolder()
{
return _curholder;
}
public void setRunning(boolean onoff)
{
runrender = onoff;
}
@Override
public void run()
{
Canvas c;
while (runrender)
{
c = null; //first clear the object buffer.
try
{
c = _curholder.lockCanvas(null); //lock the canvas so we can write to it
synchronized (_curholder)
{//we sync the thread with the specified surfaceview via its surfaceholder.
curview.onDraw(c);
}
}
finally
{
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null)
{
_curholder.unlockCanvasAndPost(c);
}
}
}
}
}