通过一个可运行到预先存在的线程中的Android / Java的运行(pass a runnable

2019-09-17 04:36发布

我有相关的以下链接一个问题: 什么的线程start()和Runnable运行之间的差异()

在这个问题上,我看到一个人创建Runnable对象,然后在两种不同的方式初始化它们。 那么,这是否意味着你可以在运行时通过周围的其他东西,这些可运行?

我想通过代码来预先存在的线程是线程的循环中执行。 我环顾四周,从我所知道的,你想创建一个像下面这样的专用运行的类:

    public class codetobesent implements Runnable  
     {  
       public void run()  
        {   
        ..morecodehere.  
        }  
      ...insertcodestuffhere  
     }  

但我怎么会通过这一个已经运行的线程? 说我试图做一个游戏,我有一些特别的东西我想渲染到其线程做。 我怎么会通过这个可运行于该线程并让它正确运行这些数据?

我当前实现我的渲染线程的是下面的,我把它关闭的教程网站,并且它一直很好,到目前为止。 但我想知道如何传递的东西,所以我可以运行比只是在预设的循环。

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);
                }
            }


        }

    }   



}

Answer 1:

一个处理器线程执行。

private void testWorker(){
        WorkerThread worker = new WorkerThread();
        worker.start();
        for (int i = 0; i < 10; i++) {
            worker.doRunnable(new Runnable() {
                public void run() {
                    Log.d("demo", "just demo");
                    try {
                        Thread.sleep(1000);//simulate long-duration operation.
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                };
            });
        }
    }

    private class WorkerThread extends HandlerThread implements Callback {

        private Handler mHandler;

        public WorkerThread() {
            super("Worker");
        }

        public void doRunnable(Runnable runnable) {
            if (mHandler == null) {
                mHandler = new Handler(getLooper(), this);
            }
            Message msg = mHandler.obtainMessage(0, runnable);
            mHandler.sendMessage(msg);
        }

        @Override
        public boolean handleMessage(Message msg) {
            Runnable runnable = (Runnable) msg.obj;
            runnable.run();
            return true;
        }

    }


文章来源: pass a runnable to a pre-existing thread to be run in Android/Java