这是什么意思lockCanvas(详细) [英] What does lockCanvas mean (elaborate)

查看:1047
本文介绍了这是什么意思lockCanvas(详细)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我心中已经被即将在Android中绘制图形。有很多的示例应用程序在那里,但有一点我始终看到的是lockCanvas。有人可以解释更接近,因为我真的不明白这一点,也因为IM想了解未来的规划很重要?

I'v been coming to drawing graphics in Android. There's a lot of sample applications out there, but one thing I always seeing is lockCanvas. Can someone explain it closer since I really don't get it and also because im think it's important to understand to future programming?

一个例子:

try { 
    c = panel_thread.getHolder().lockCanvas(null);
    synchronized (panel_thread.getHolder()) {
        panel_thread.update();
        panel_thread.onDraw(c);
    }
}

这是我现在。 我应该如何跨preT这是否正确?什么是同步的吗?为什么是重要的画布对象分配到getHolder和lockCanvas?

This is what I have for now. How should I interpret this correct? What does synchronized do? Why is it important to assign the canvas-object into a getHolder and lockCanvas?

这部分也是令人困惑的:

This part is also confusing:

panel_thread.getHolder().unlockCanvasAndPost(c);

为什么这个必要吗?我真的需要仔细的解释。 :)

Why is this necessary? I really need a closer explanation. :)

推荐答案

同步表示只有一个线程可以同时执行的code表示块。

synchronized indicates that only one thread can execute that block of code at a time.

在这个例子中,没有同步块,多个线程可以在同一时间绘制图形,其结果可能是混乱的。因此,同步确保只有一个线程可以同时借鉴。

In this example, without the synchronized block, multiple threads could draw graphics at the same time, and the results could be messy. Thus, synchronized ensures that only one thread can draw at a time.

lockCanvas()创建表面积,你会写。这就是所谓的理由 lockCanvas()是因为,直到调用 unlockCanvasAndPost()没有其他的code可以调用 lockCanvas()并写入到地面,直到你的code就结束了。

lockCanvas() creates a surface area that you will write to. The reason it's called lockCanvas() is because until you call unlockCanvasAndPost() no other code can call lockCanvas() and write to the surface until your code is finished.

在一般情况下,锁是很重要的理解,特别是当它涉及到多线程编程。锁是一个同步原语,用来防范资源/ code。通过多线程并发访问。它得到它的名字,因为它的表现很像一个物理锁。通常一个线程可以获取锁,直到它释放锁,其它线程不能获得锁。一个潜在的疑难杂症要使用锁是它的滥用可能导致死锁的情况,即线程离开等待锁,而且它永远不会释放。

In general, locks are important to understand, specifically when it relates to multithreaded programming. A lock is a synchronization primitive that is used to guard against simultaneous accessing of resources/code by multiple threads. It gets it's name because it behaves much like a physical lock. Generally one thread can obtain a lock, and until it releases the lock, no other thread can obtain the lock. One potential gotcha to using a lock is that misuse of it can lead to a "dead lock" situation, where threads are left waiting for the lock, and it's never released.

这篇关于这是什么意思lockCanvas(详细)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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