Android 中的线程是如何工作的

发布于 2024-12-11 21:54:42 字数 2814 浏览 0 评论 0原文

你能解释一下线程是如何工作的吗?实际上它现在工作得很好,但我想了解背后的逻辑。我认为它让我有机会同时进行两次计算,这样我就不必等待第一次计算和。但如何使用呢?第一个计算放在哪里,第二个计算放在哪里?在 C++ 中,当我拆分它时,我必须获取进程 ID,然后我必须使用 if 之类的 if(pID==1){//first 计算}

我的类是: 我有一个 A 级,它是主要活动 a ClassB:用作扩展到 SurfaceView 的竞争视图 和一个ClassC:用作扩展至Thread的游戏循环

,它们是: A类:

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

B类:

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

C类:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满意归宿 2024-12-18 21:54:42

你是想问 Android 中线程一般是如何工作的?

在 C++ 中,我假设您使用线程作为并行处理工具,以将重复工作的负载分配到多个处理器上。

在Android中,除非你使用的是多核设备和像Tegra平台这样的特定框架,否则这种线程处理实际上是浪费时间。在 Android 中,最好使用线程来执行您不想冒险在逻辑线程或 UI 线程上执行的任务。

在你的例子中,我假设你正在使用 android 构建游戏,我建议使用两个线程:一个逻辑线程和一个 UI 线程。让这些异步运行,您的游戏应该可以顺利运行。

抱歉,我无法提供更多帮助,因为我不确定您的问题到底是什么。

有关 Android 中线程的更多信息,请访问 Android 开发人员

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

终陌 2024-12-18 21:54:42

在C类中,你不需要调用onDraw,它是由主活动调用的(如果没有,你做错了什么)。在主活动中启动逻辑线程。所以你得到了两条线索——绘图和逻辑。
如果你在ClassC中调用onDraw,然后编写ClassZ扩展Thread,那就会计算一些逻辑时刻。
在 C 中,您这样做是为了了解您所在的线程。在 Java 中,一个 Thread.run 在分叉线程中执行一项工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文