通过一个可运行至pre-现有的线程在Android中/ Java的运行 [英] pass a runnable to a pre-existing thread to be run in Android/Java

查看:158
本文介绍了通过一个可运行至pre-现有的线程在Android中/ Java的运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于以下链接的问题:<一href=\"http://stackoverflow.com/questions/8579657/java-whats-the-difference-between-thread-start-and-runnable-run\">Java:什么是线程start()和Runnable运行()之间的差异

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

我想通过code到A preexisting线程中的线程的循环中执行。我环顾四周,从我可以告诉,你要创建如下所示的专用运行的类:

公共类codetobesent实现Runnable
     {
       公共无效的run()
        {
        ..更多codehere。
        }
      ...插入codestuffhere
     }

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

我的当前实现我的渲染线程的是下面的,我把它关闭的教程网站,它已顺利完成,那么工作pretty。但我想知道如何传递的东西,所以我可以运行比只是在preSET的循环。

 类RenderThread继承Thread
{
私人SurfaceHolder _curholder;
私人UserView CURVIEW;
私人布尔runrender = FALSE;    公共RenderThread(SurfaceHolder持有人,UserView thisview)
    {//构造函数 - 当您创建该对象的新实例,调用此方法。
        CURVIEW = thisview;
        _curholder =持有人;
    }    公共SurfaceHolder getThreadHolder()
    {
        返回_curholder;
    }
    公共无效setRunning(布尔ONOFF)
    {
        runrender = ONOFF;
    }
    @覆盖
    公共无效的run()
    {
        帆布℃;
        而(runrender)
        {
            C = NULL; //先清除对象缓存。
            尝试
            {
              C = _curholder.lockCanvas(NULL); //锁定画布,所以我们可以写入
              同步(_curholder)
              {//我们同步通过其surfaceholder指定surfaceview线程。
                  curview.onDraw(C);
              }            }            最后
            {
              //在最终做到这一点,这样如果有异常抛出
              //上面的过程中,我们不留在表面
              //不一致的状态
                如果(C!= NULL)
                {
                    _curholder.unlockCanvasAndPost(C);
                }
            }
        }    }}


解决方案

一个处理器线程执行。

 私人无效testWorker(){
        的WorkerThread工人=新的WorkerThread();
        worker.start();
        对(INT I = 0; I&小于10;我++){
            worker.doRunnable(新的Runnable(){
                公共无效的run(){
                    Log.d(演示,只是演示);
                    尝试{
                        视频下载(1000); //模拟长时间的运行。
                    }赶上(InterruptedException的E){
                        e.printStackTrace();
                    }
                };
            });
        }
    }    私有类的WorkerThread扩展HandlerThread实现回调{        私人处理器mHandler;        公众的WorkerThread(){
            超(工人);
        }        公共无效doRunnable(Runnable接口可运行){
            如果(mHandler == NULL){
                mHandler =新的处理程序(getLooper(),这一点);
            }
            消息味精= mHandler.obtainMessage(0,可运行);
            mHandler.sendMessage(MSG);
        }        @覆盖
        公共布尔的handleMessage(消息MSG){
            可运行的可运行=(Runnable接口)msg.obj;
            runnable.run();
            返回true;
        }    }

I have a question related to the following link: Java: 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);
                }
            }


        }

    }   



}

解决方案

A Handler Thread implementation.

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

    }

这篇关于通过一个可运行至pre-现有的线程在Android中/ Java的运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆