第五章(5)Libgdx应用框架之线程

Android游戏开发群:290051794
Libgdx游戏开发框架交流群:261954621 

 

所有的ApplicationListener方法都会在同一线程中调用,这个线程是OpenGL调用的渲染线程。对于大多数程序来说足够实现逻辑更新和ApplicationListener.render()中的渲染,在渲染进程中。

任何图形的操作直接涉及OpenGL在渲染线程中执行。在不同的线程中这样做会导致未知的问题。这是因为OpenGL环境仅仅会在渲染进程中激活。在不同进程中执行在很多Android设备中出现问题。所以不支持。

使用Application.postRunnable()将其他线程的数据传递到渲染线程。在ApplicationListener.render()调用之前,会在渲染线程的Runnable中运行代码。

 

 

 

 

 

 

 

 

 

new Thread(new Runnable() {

   @Override

   public void run() {

      // do something important here, asynchronously to the rendering thread

      final Result result = createResult();

      // post a Runnable to the rendering thread that processes the result

      Gdx.app.postRunnable(new Runnable() {

         @Override

         public void run() {

            // process the result, e.g. add it to an Array<Result> field of the ApplicationListener.

            results.add(result);

         }

      });

   }

}).start();


 

 作者:宋志辉 
出处:http://blog.csdn.net/song19891121
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 
支持: 新浪微博 腾讯微博

 

 

 

原文地址:https://www.cnblogs.com/javawebsoa/p/3074265.html