Estoy tratando de crear un nuevo subproceso en el onCreate() de una actividad, pero en lugar de crear un nuevo subproceso y ejecutar el código ejecutable en él, el código ejecutable se está ejecutando en el hilo principal de mi programa. Un nuevo hilo nunca parece ser creado.Android: Nuevo subproceso Ejecutable se ejecuta en el subproceso principal
El onCreate() Código:
@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();
}
El código ejecutable:
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();
}
}
}
}
El seguimiento de pila que tuve cuando hice una pausa el código cuando se estaba ejecutando:
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)
¿Qué es el hilo conductor [<5> Binder Thread # 1] (Suspendido)? – Jack
Jack No sé por qué dice "Subproceso [<5> Subproceso Binder # 1] (Suspendido)". Supongo que pause accidentalmente ese hilo también cuando estaba copiando el seguimiento de la pila. Realmente no importa, mi pregunta fue respondida de todos modos. ¡Gracias! – HappyCoder86