Evren Ozturk
Evren Ozturk

Reputation: 928

How works thread in android

Can you explain me how thread is working. Actually it's working just fine right now but I want to get the logic behind. I think it gives me a chance to do 2 calculation in same time so I don't have to wait for first calculation to and. But how to use it? Where to put first calculation and where to put second calculation? In C++ I have to get process ID when I'm splitting it and then I have to use an if something like that if(pID==1){//first calculation}

My classes are: I have a ClassA which is main activity a ClassB: using as contend view extended to SurfaceView and a ClassC: using as game loop extended to Thread

and here they are: ClassA:

public class ClassA extends Activity{ 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                               WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new ClassB(this));
    }
}

ClassB:

public class ClassB extends SurfaceView{
//Variables.......................
    private ClassC GameLoop;
//Constructor.....................    
    public GameView(final Context context) {
        super(context);

        GameLoop = new ClassC(this);
        Holder=getHolder();
        Holder.addCallback(new SurfaceHolder.Callback() {
            public void surfaceDestroyed(SurfaceHolder holder) {
                // TODO Auto-generated method stub
            }
            public void surfaceCreated(SurfaceHolder holder) {
                // TODO Auto-generated method stub
            }
            public void surfaceChanged(SurfaceHolder/// blaa bla
                // TODO Auto-generated method stub
            }
        });
        this.setOnTouchListener(new OnTouchListener()
        {    
            //@Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
            }
        });
    }
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.GREEN);
        //Using canvas and drawing something on it.
    }
}

ClassC:

public class ClassC extends Thread {//GameLoop
    private ClassB view;
    private boolean running = false;

    public ClassC(ClassB view) {
        this.view = view;
    }
    public void setRunning(boolean run) {
          running = true;
    }
    @Override
    public void run() {
        while (running) {
           Canvas c;
           try {
               c = view.getHolder().lockCanvas();
               synchronized (view.getHolder()) {
                   view.onDraw(c);// Here I call onDraw to draw everything
               }
           } finally {
               if (c != null) {
                   view.getHolder().unlockCanvasAndPost(c);
               }
           }
        }
    }
}

Upvotes: 1

Views: 690

Answers (2)

Michael Allen
Michael Allen

Reputation: 5838

Are you asking how threads in general work in android?

In C++ I assume you were using Threads as a parallel processing tool, to split the load of a repetitive piece of work across a number of processors.

In Android, unless you are using a multicore device and a specific framework like the Tegra platform, this kind of thread processing is in fact a waste of time. In Android it is better to use threads to perform tasks you don't want to risk performing on the logic thread or the UI thread.

In your case I assume you are building a Game using android, I would recommend using two threads: a logic thread, and a UI thread. Have these running asynchronously and your game should run smoothly.

Sorry I can't help more, as I'm not sure exactly what your question is.

More info on Threads in android can be found here Android Developers

Upvotes: 2

IronBCC
IronBCC

Reputation: 429

In classC you dont need call onDraw, it called by main activity(if no, you do some thing wrong). Start logic thread in main activity. So you get two thread - draw and logic. If you what call onDraw in ClassC, then write ClassZ extend Thread, that will calculate some logic moments. In C you do this, to understand in what thread you in. In Java - one Thread.run do one job, in forked thread.

Upvotes: 0

Related Questions