Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about "implements Runnable"

Hello have only a few days with Java and android here. I am a bit confused about exactly how the "implements runnable" actually works example:

public class DrawableSurfaceView extends SurfaceView implements Runnable {

[...]

public void resume(){
    isRunning = true;
    mThread = new Thread(this);
    mThread.start(); //start the animation
    parseParameters(); //<== Here is my problem
}

public void run() {
    while (isRunning == true){
        if (!mHolder.getSurface().isValid()) {
         continue;
        }
        Canvas canvas = mHolder.lockCanvas();
        canvas.drawARGB(255, 0, 0, 0);
        canvas.drawPath(PenPath, PenPaint);
        canvas.drawPath(CursorPath, CursorPaint);
        mHolder.unlockCanvasAndPost(canvas);
    }
}
public void parseParameters() {
  [...]
  [ The rest of my code here  modifying PenPath and CursorPath, etc ]

}

I am embarrassed to ask, but I thought that after mThread.start(); a new thread would be started running a loop in the run method. instead what I get is the run method only executed after my parseParameters() method terminates. What I wanted to achieve is to have the canvas on a drawing loop thread and externally modify the parameters of the drawing paths to generate my animation. I am sure this is very elemental, but I have been unable to understand this for a few hours. The docs are not helping.

Any pointer would help a lot. Cheers guys!

like image 950
Martin Ansat Avatar asked Nov 25 '25 11:11

Martin Ansat


1 Answers

The meaning of implements Runnable is that this class reacts to the methods defined in the Runnable interface, and those may be passed to the constructor of Thread as you do.

Now, a new thread is not immediately executed, and it is very possible that the current thread will continue to execute some function before the system will switch to the other thread's context.

like image 89
MByD Avatar answered Nov 28 '25 00:11

MByD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!