线程阻塞了我的 Android UI

发布于 2024-11-13 05:21:39 字数 1477 浏览 0 评论 0原文

我的 Android 应用程序中的 java 线程有问题。我的嵌套线程阻塞了我的 UI,我该如何解决这个问题?

MyClass.java

package com.knobik.gadu;

import android.util.Log;

public class MyClass {

    public void StartTheThread() {

        Thread Nested = new Thread( new NestedThread() );
        Nested.run();
    }

    private class NestedThread implements Runnable {

        public void run() {

            while (true) {
                Log.d( "DUPA!", "debug log SPAM!!" );
            }

        }

    }

}

这就是我运行它的方式:

package com.knobik.gadu;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class AndroidGadu extends Activity {
    public static final String LogAct = "AndroidGadu";

    public void OnClickTest(View v) {

        MyClass test = new MyClass();
        test.StartTheThread();

    }


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



    }
}

你能帮助我吗?我真的被困住了;)

I have a problem with java Threads in my Android app. My nested thread blocks my UI, how can i resolve this?

MyClass.java

package com.knobik.gadu;

import android.util.Log;

public class MyClass {

    public void StartTheThread() {

        Thread Nested = new Thread( new NestedThread() );
        Nested.run();
    }

    private class NestedThread implements Runnable {

        public void run() {

            while (true) {
                Log.d( "DUPA!", "debug log SPAM!!" );
            }

        }

    }

}

and this is how i run it:

package com.knobik.gadu;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class AndroidGadu extends Activity {
    public static final String LogAct = "AndroidGadu";

    public void OnClickTest(View v) {

        MyClass test = new MyClass();
        test.StartTheThread();

    }


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



    }
}

Could you help me? I'm literaly stuck ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦与时光遇 2024-11-20 05:21:39

我的嵌套线程阻塞了我的 UI

您需要使用 .start() 而不是 .run()。也就是说,替换

Nested.run();

Nested.start();

(run 只是一个普通方法。start() 是实际生成一个新线程的方法,该线程又运行 run代码>)

My nested thread blocks my UI

You need to use .start() instead of .run(). That is, replace

Nested.run();

with

Nested.start();

(run is just an ordinary method. start() is the method that actually spawns a new thread, which in turn runs run)

心意如水 2024-11-20 05:21:39

这段代码有一个问题:

while (true) {
    Log.d( "DUPA!", "debug log SPAM!!" );
}

繁忙的等待占用了CPU。

This code is a problem:

while (true) {
    Log.d( "DUPA!", "debug log SPAM!!" );
}

A busy wait hogs the CPU.

橪书 2024-11-20 05:21:39
package com.knobik.gadu;

import android.util.Log;

public class MyClass {

    public void StartTheThread() {
        Thread Nested = new Thread( new NestedThread() );
        Nested.run(); // Change this to Nested.start();
    }

    private class NestedThread implements Runnable {

        public void run() {

            while (true) { // infinite loop logical error
                Log.d( "DUPA!", "debug log SPAM!!" );
            }
        }
    }
}
package com.knobik.gadu;

import android.util.Log;

public class MyClass {

    public void StartTheThread() {
        Thread Nested = new Thread( new NestedThread() );
        Nested.run(); // Change this to Nested.start();
    }

    private class NestedThread implements Runnable {

        public void run() {

            while (true) { // infinite loop logical error
                Log.d( "DUPA!", "debug log SPAM!!" );
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文