Home > OS >  Weird printing behaviour in java threads
Weird printing behaviour in java threads

Time:12-05

I am new to coding and wrote a very simple java code using the concept of threads. I tried printing two statements "good morning" and "welcome" infinitely using two different threads from two classes to see the behaviour of the threads. However during multiple runs I noticed that sometimes in the output the first line showed incomplete text although not in every single run. Can someone explain what's going on?

    class Mythread1 extends Thread{
    public void run(){
        while (true) {
            System.out.println("good morning");
        }
    }
}
class Mythread2 extends Thread{
    public void run(){
        while (true) {
            System.out.println("welcome");
        }
    }
}
public class threads{
    public static void main(String[] args) {
        Mythread1 t1=new Mythread1();
        Mythread2 t2=new Mythread2();
        t1.start();
        t2.start();
    }
}
rning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome

I paused the output since it was infinite.

Running again

ng
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning
good morning

CodePudding user response:

I guess you expected a thing such as:

good morning
good morning
welcome
good morning
welcome
good morning
welcome
welcome
...

But the thing happen here is thread is not stay forever and executed parallel with each other. It execute then jump, execute then jump, .... Beside, the while(true) is execute too fast that will confuse you at the first time, if you set thread.sleep(10) result will much observables

Try this:

class Mythread1 extends Thread{
    public void run(){
        while (true) {
            System.out.println("good morning");
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }}
    }
}
class Mythread2 extends Thread{
    public void run(){
        while (true) {
            System.out.println("welcome");
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
public class threads{
    public static void main(String[] args) {
        Mythread1 t1=new Mythread1();
        Mythread2 t2=new Mythread2();
        t1.start();
        t2.start();
    }
}

CodePudding user response:

Is it the same if you change your code to,

class Mythread1 extends Thread{
    public void run(){
        while (true) {
            System.out.println("good morning");
            try {
                Thread.sleep(100);
            catch(InterruptedException e) {
                System.out.println("Interrupted");
            }
        }
    }
}
class Mythread2 extends Thread{
    public void run(){
        while (true) {
            System.out.println("welcome");
            try {
                Thread.sleep(100);
            catch(InterruptedException e) {
                System.out.println("Interrupted");
            }
        }
    }
}

A while(true) loop is something executed very fast.
Moreover, you could use PrintWriter instead of the slow System.out.println.

class Mythread1 extends Thread{
    public void run(){
        PrintWriter writer = new PrintWriter(System.out);
        while (true) {
            writer.println("good morning");
            writer.flush();
        }
    }
}
class Mythread2 extends Thread{
    public void run(){
        PrintWriter writer = new PrintWriter(System.out);
        while (true) {
            writer.println("welcome");
            writer.flush();
        }
    }
}
  • Related