Reputation:
i have a Question, first a part of my code.
myThread thread1;
myThread2 thread2;
if (firstThread == null) {
(thread1 = new myThread()).start();
Toast.makeText(CreateService.this, "first thread started",
Toast.LENGTH_LONG).show();
}
if (secondThread== null) {
(thread2 = new myThread2()).start();
Toast.makeText(CreateService.this, "second thread started",
Toast.LENGTH_LONG).show();
}
My Question : Are the 2 Threads running know at the same time ? Or one after another ? How would look a code where they are running at the same time ?
Thanks in advance !
Upvotes: 0
Views: 1033
Reputation: 2916
You are currently in your Main Thread:
MAIN
You start thread1:
MAIN
startsThread1 -> THREAD1
Toast thread 1 executes
You start thread2:
MAIN
startsThread1 -> THREAD1
Toast thread 1 executes
startsThread2 -> -> THREAD2
Toast thread 2 executes
Your Main Thread, thread1 and thread2 will be executed at the same time. Your toasts will be shown while both of them execute, not afterwards, because you present the toast on the Main thread (which is by the way the only thread where you should do UI changes)
Upvotes: 1
Reputation: 1068
Thread runs in Parallel so if you creating more than two thread all will be run at same time, To know they are running at same time/ parallel Log the message from each thread like
From thread 1
for(int count=0; count < 100; count++)
Log.d("Thread 1":"Count : "+count);
From thread 2
for(int count=0; count < 100; count++)
Log.d("Thread 2":"Count : "+count);
Upvotes: 0