In my opinion this is the logic of execution for the 2 threads (main and SpecialThread):
1. main is run
2. a new thread "thread" is created.
3. thread is assigned a new SpecialThread object.
4. the new thread is started.
5. the **** line is printed.
Now the confusion begins for me. In my opinion and from the lesson about thread, once the new thread is created and started, then the computer executes alternatively 1 line from each code.
BUT the problem is that we have 2 for loops. Now, it executes first 1 WHOLE for loop and then moves to the next for loop?
OR it executes 1 LINE of the first for loop then moves the THE OTHER LINE from the OTHER FOR LOOP ?
So, what would step 6 be? From the output of the program I understand the following:
6. first stack trace from element;
7. first stack trace from element1;
8. second stack trace from element;
9. second stack trace from element1;
Have I assumed correctly?
package com.codegym.task.task16.task1604;
/*
Displaying a stack trace
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new SpecialThread());
thread.start();
System.out.println("*****************");
for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
System.out.println(element);
}
}
public static class SpecialThread implements Runnable{
@Override
public void run(){
for (StackTraceElement element1: Thread.currentThread().getStackTrace()) {
System.out.println(element1);
}
}
}
}