卡在后台线程的while循环

卡在后台线程的while循环

本文介绍了Java:卡在后台线程的while循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LibGDX AsyncExecutor,并且在后台线程中调用以下函数. 'tasksForTheMainThread'是Runnable的静态数组,它在每次调用主线程中的update函数期间执行尚未执行的元素. 'modelBuilder'的函数'createBox'创建并返回'Model'类的对象.

I'm using LibGDX AsyncExecutor and the following function is called in a background thread. 'tasksForTheMainThread' is the static array of Runnable, which executes its not yet executed elements during each call of the update function in the main thread. The function 'createBox' of 'modelBuilder' creates and returns an object of the class 'Model'.

简要说明一下,该代码在第二个线程中执行,并发送一段代码(函数"run()")以在第一个线程中使用.发送完之后,第二个线程将被冻结,直到"run()"中的代码完成并创建Model对象为止(或者至少应该是这样).

Briefly explaining, this code executes in the second thread and sends a piece of code (function 'run()') to be used in the first thread. After it's sent, the second thread is frozen until the moment the code in "run()" is completed and the Model object is created (or at least it's supposed to be like that).

但是,仅当while循环(仅等待直到在主线程中创建对象)包含记录位(Gdx.app.log("TAG","2");)时,它才能按预期工作.当它为空时,即使创建了Model对象,第二个线程也将永远冻结,并且永远不会到达点A".

However, it works as expected only when the while loop (which just waits until the object is created in the main thread) contains the logging bit (Gdx.app.log("TAG","2");). When it's empty, the second thread freezes forever and never reaches 'point A' even after the creation of the Model object.

为什么日志记录会如何影响它?为何没有它,程序无法运行?

Why and how logging can influence that? And why isn't the programme working without it?

void secondThreadFunction()
{
    Model model = null;

    ChunkManager.tasksForTheMainThread.add(new Runnable()
    {
                    @Override
                    public void run()
                    {
                            model = modelBuilder.createBox(size.x, size.y, size.z, GL20.GL_LINES,
                                    new Material(ColorAttribute.createDiffuse(Color.YELLOW)),
                                    VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
                    }
     });

     while (model == null)
     {
            //Gdx.app.log("TAG","2");
     }

      //point A
}

推荐答案

您无法修改已捕获到内部类的局部变量.由于已捕获"该值,因此您将在该值的副本上进行操作,并且该值永远不会为非null,从而导致无限循环.另外请注意,您正在紧张地等待.最好使用 Future .

You cannot modify a local variable that has been captured to an inner class. Since it has been "captured", you will operate on a copy of the value and it will never be non-null, causing the endless loop. Also note that you are busy-waiting in a tight loop. It might be better to use a Future of some kind.

void secondThreadFunction()
{
    AtomicReference<Model> model = new AtomicReference<Model>();

    ChunkManager.tasksForTheMainThread.add(new Runnable()
    {
                    @Override
                    public void run()
                    {
                            model.set(modelBuilder.createBox(size.x, size.y, size.z, GL20.GL_LINES,
                                    new Material(ColorAttribute.createDiffuse(Color.YELLOW)),
                                    VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal));
                    }
     });

     while (model == null)
     {
            //Gdx.app.log("TAG","2");
     }

      //point A
}

这篇关于Java:卡在后台线程的while循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:02