Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: New Thread Runnable Executes in Main Thread

I'm trying to create a new thread in the onCreate() of an activity, but instead of creating a new thread and executing the runnable's code in it, the runnable code is executing in the main thread of my program. A new thread never seems to be created.

The onCreate() code:

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.monster_layout);
    runningMonsterViewActivity = this;
    Thread.currentThread().setName("Main Thread");
    Log.v(TAG, "onCreate() has run");
    Thread genThread = new Thread(new TestRunnable());
    genThread.run();
}

The runnable code:

import android.os.Handler;
import android.util.Log;

public class TestRunnable implements Runnable{
String TAG = "TestRunnable";

    public TestRunnable()
    {
    //  mainThreadHandler = h;
    }
    @Override
    public void run() {
        for(int i=0; i < 1000; i++){
            Log.v(TAG, new Integer(i).toString());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

The stack trace I got when I paused the code when it was executing:

DalvikVM[localhost:8621] (Suspended)    
    Thread [<1> Main Thread] (Suspended)    
        VMThread.sleep(long, int) line: not available [native method]   
        Thread.sleep(long, int) line: 1306  
        Thread.sleep(long) line: 1286   
        TestRunnable.run() line: 18 
        Thread.run() line: 1096 
        MonsterViewActivity.onCreate(Bundle) line: 49   
        Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1047   
        ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2627  
        ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679   
        ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 
        ActivityThread$H.handleMessage(Message) line: 2033  
        ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
        Looper.loop() line: 123 
        ActivityThread.main(String[]) line: 4627    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 521  
        ZygoteInit$MethodAndArgsCaller.run() line: 868  
        ZygoteInit.main(String[]) line: 626 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<6> Binder Thread #2] (Suspended)   
    Thread [<5> Binder Thread #1] (Suspended)   
    Daemon System Thread [<3> Signal Catcher] (Suspended)   
    Daemon System Thread [<2> HeapWorker] (Suspended)   
like image 437
HappyCoder86 Avatar asked Aug 12 '11 18:08

HappyCoder86


People also ask

Does runnable Run main thread?

This Runnable runs on the current thread, i.e. the thread that invokes this code. It doesn't magically create, or constitute, another thread. Runnable. run() is only a method call.

Does Android service run on main thread?

Caution: A service runs in the main thread of its hosting process; the service does not create its own thread and does not run in a separate process unless you specify otherwise. You should run any blocking operations on a separate thread within the service to avoid Application Not Responding (ANR) errors.

Why should you avoid to run non UI code on the main thread?

All Android apps use a main thread to handle UI operations. Calling long-running operations from this main thread can lead to freezes and unresponsiveness. For example, if your app makes a network request from the main thread, your app's UI is frozen until it receives the network response.

Which action should be performed on the main thread Android?

The main thread is responsible for dispatching events to the appropriate user interface widgets as well as communicating with components from the Android UI toolkit. To keep your application responsive, it is essential to avoid using the main thread to perform any operation that may end up keeping it blocked.


1 Answers

You just need to change genThread.run(); to genThread.start();

Right now, your code calls the run() method from the main thread. start() will actually start a new thread and will execute the run() method on that thread, which is the desired behaviour.

like image 187
dlev Avatar answered Sep 20 '22 19:09

dlev