Tanvir Arafat
Tanvir Arafat

Reputation: 41

Is there any way to find the execution flow of java code?

  public class Threads1 {
    int x = 0;
    public class Runner implements Runnable {
        public void run(){
            int current = 0;
            for(int i = 0; i<4; i++){
                current = x;
                System.out.println(current + ", ");
                x = current + 2;
            }
        }
    }

    public static void main(String[] args) {
        new Threads1().go();
    }

    public void go(){
        Runnable r1 = new Runner();
        new Thread(r1).start();
        new Thread(r1).start();
    }
}

I want to find the execution step of this code ,Is there any way that can show me the execution flow in my consol?

Upvotes: 0

Views: 1271

Answers (3)

Przemysław Moskal
Przemysław Moskal

Reputation: 3609

One of the possibilities is to create a field in the class that would differentiate both threads to make you sure which thread prints the values to the console in the moment (you should contain this additional value in the informations you print to the console in the "for" loop to know if the first or the second thread has performed activity printed to the console).

Another possibility is using debugger.

Upvotes: 0

ProfBits
ProfBits

Reputation: 219

Java Threads
If you have several threads running at once ther is no way to predict which one of them finishes first or when something is done, like to differen programms. Different threads are executed by different cores of you CPU and therefore completely independend from oneanother, this affects computation speed and order (branchprediction in you CPU plays one role here). This can lead to pretty difficult bugs to find because they only happen sometimes.

Your Problem
The execution order of the threads is not determend a compile time it's decided by you CPU at runtime and is not consistent because the threads do not know they have a partner.

Solution
To display the order at which it happend just insert System.out.println statements in your methodes and create so a sort of log what happened in you console.

Controled Threads with Synchronized
If you use threads, Java supports the synchronized keyword. Synchronized allows you to get a bit of control over several threads. For example only one thread at a time can interact with a variable or methode. For more information just google Java synchronized threads.

Upvotes: 0

Sweeper
Sweeper

Reputation: 271040

Print Statements

Put System.out.println() statements in appropriate places in your code. For example, put

System.out.println("Entering a for loop");

before a for loop to know when the for loop is entered.

Debugger

Find out how to set a breakpoint in your IDE. Set a breakpoint at start of the portion of code that you don't understand. When that line is going to be executed, the program will pause and allow you to examine the values in variables, the stack trace, the threads, etc. You can also find out which thread is the current line running on in the debugger.

Additionally, find "Step in", "Step over" and "Step out" buttons on the debugger. These will allow you to "step through" your code line by line, so as to allow you to see the path that execution takes.

Upvotes: 1

Related Questions