lockCanvas and synchronization in Android

荒凉一梦 提交于 2019-12-13 20:59:24

问题


@Override
   public void run()
   {
     Canvas canvas =  null; // used for drawing               {
     while  (threadIsRunning)
     {
        try
        {
         canvas = surfaceHolder.lockCanvas(null);  // line 1                               

           synchronized(surfaceHolder)             // line 2

                { //do something .....

                }

           }
        }
      }

According to many post and blogs lockCanvas will make a lock on canvas so that multiple access to canvas is not possible and synchronized block is used so that no other thread draw on canvas ..

Ques is if other thread is prevented from having a canvas by lock canvas by forcing it to wait . Then what is need of synchronized block as thread cannot reach this block as it is blocked


回答1:


I think, if lockCanvas cannot obtain lock it will just return null and execution will continue.



来源:https://stackoverflow.com/questions/17639258/lockcanvas-and-synchronization-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!