Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program execution is non sequential. Why?

I was fooling around with how I could set up my encapsulation.

But my program is executing in an unexpected order. Here is my rather simple code:

The "Main":

package research.debug;

public class Main {

public static void main(String[] args) {

    Boolean b = Boolean.TRUE ;      

    Debug.black.printVariable( b, "b" ) ;
    Debug.red.printVariable( b, "b" ) ;

    System.out.println( "SUPPOSED to be inbetween..." ) ;

    Debug.black.println( "Hello" ) ;
    Debug.red.println( "Howdie" ) ;

}

}

"Debug":

package research.debug;

public class Debug {

public static final Output black = new Output( Output.BLACK ) ;
public static final Output red = new Output( Output.RED ) ;

}

And lastly, "Output":

package research.debug;

public class Output {

public static final int BLACK = 0 ;
public static final int RED = 1 ; 

private int outputMode = 0 ;

public Output( int outputMode ) {

    this.outputMode = outputMode ; 

}

public void println( String msg ) {

    if( outputMode == Output.BLACK ) {
        System.out.println( "Printed with black font: " + msg ) ;
    } else {
        System.err.println( "Printed with red font: " + msg ) ;
    }

}

public void printVariable( Object variable, String variableName ) {

    println( variableName + " = \"" + variable + "\"" ) ;

}

}

And the expected output would be:

Printed with black font: b = "true"

Printed with red font: b = "true"

SUPPOSED to be inbetween...

Printed with black font: Hello

Printed with red font: Howdie

But is instead out of the expected order, like this:

Printed with black font: b = "true"

SUPPOSED to be inbetween...

Printed with black font: Hello

Printed with red font: b = "true"

Printed with red font: Howdie

What's happening?

EDIT: Sometimes the "Supposed to be in between" message moves around. Without me changing the code.

like image 693
CrazyPenguin Avatar asked Feb 03 '11 21:02

CrazyPenguin


People also ask

What is a sequential flow?

October 8, 2018 by Lisa League. This is a programmatic concept describing a situation in which events or processes must occur in a specific order with regard to people and/or objects.

What is sequentially programming?

Sequential programming is the process of programming multiple devices in a chain, one device at a time. After the process of programming the first device in the chain is complete, the next device is programmed. This sequence continues until all specified devices in the JTAG chain are programmed.

Is sequential program in execution?

Sequential execution means that each command in a program script executes in the order in which it is listed in the program. The first command in the sequence executes first and when it is complete, the second command executes, and so on.


2 Answers

System.out is buffered and System.err is not, they are two different streams, and some of your messages go to one, some to the other.

Hence, these mixed messages may not appear in the expected order as the prints to System.out are delayed until the buffer is flushed (manually or automatically), whereas those to System.err should be written right away.

You can flush a stream manually by calling the flush() method.

like image 171
Mike Tunnicliffe Avatar answered Oct 05 '22 03:10

Mike Tunnicliffe


You are printing to System.err and System.out. Try to print only to System.out or use System.out.flush() to flush the buffers.

like image 39
Alexandru Avatar answered Oct 05 '22 04:10

Alexandru