{"id":921,"date":"2011-05-12T09:50:13","date_gmt":"2011-05-12T15:50:13","guid":{"rendered":"http:\/\/www.philhassey.com\/blog\/?p=921"},"modified":"2011-05-12T09:54:42","modified_gmt":"2011-05-12T15:54:42","slug":"android-threading-tips","status":"publish","type":"post","link":"https:\/\/www.philhassey.com\/blog\/2011\/05\/12\/android-threading-tips\/","title":{"rendered":"Android threading tips"},"content":{"rendered":"<p>Hey,<\/p>\n<p>I still don&#8217;t completely understand how the <a href=\"http:\/\/developer.android.com\/guide\/topics\/fundamentals\/processes-and-threads.html\">Android threading works<\/a>.  So that explains some of why my code sometimes has trouble.  Here&#8217;s a good article on <a href=\"http:\/\/www.javaworld.com\/javaworld\/jw-10-2001\/jw-1012-deadlock.html\">deadlocking in Java<\/a>.<\/p>\n<p>In a Activity + GLSurfaceView there are at least two threads in operation.  In my code I synchronize all my native methods.  Some of my native methods have to call back to Java to load textures, play music, etc.  I&#8217;ve found in certain cases a deadlock occurs.  My guess is that the deadlock happens when:<\/p>\n<p>&#8211; I&#8217;m receiving a touch event and I&#8217;m trying to call my native code with the event data<br \/>\n&#8211; I&#8217;m rendering a scene and it is trying to load a texture.<\/p>\n<p>My guess is that loading a texture is trying to acquire a lock that the Activity thread has locked, while the Activity is trying to send an event to the native code which is locked.  <\/p>\n<p>Here are ways to push code onto the other thread so that you can reduce the amount of synchronized type deadlocks.<\/p>\n<p><strong>Putting a item on the Activity \/ UI thread<\/strong><\/p>\n<pre>\r\n            myActivity.runOnUiThread(new Runnable(){\r\n                public void run() {\r\n                    \/* do something in the Activity UI \/ thread *\/\r\n                }});\r\n<\/pre>\n<p><strong>Putting an item onto the GLSurfaceView thread<\/strong><\/p>\n<pre>\r\n        myGLSurfaceView.queueEvent(new Runnable(){\r\n            public void run() {\r\n                \/* do something on the GLSurfaceView thread *\/\r\n        }});\r\n<\/pre>\n<p>Anyway, hope that helps!<br \/>\n-Phil<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, I still don&#8217;t completely understand how the Android threading works. So that explains some of why my code sometimes has trouble. Here&#8217;s a good article on deadlocking in Java. In a Activity + GLSurfaceView there are at least two threads in operation. In my code I synchronize all my native methods. Some of my [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-921","post","type-post","status-publish","format-standard","hentry","category-android"],"_links":{"self":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/comments?post=921"}],"version-history":[{"count":3,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/921\/revisions"}],"predecessor-version":[{"id":924,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/921\/revisions\/924"}],"wp:attachment":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/media?parent=921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/categories?post=921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/tags?post=921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}