Difference in thread execution in Java

6

See the execution of two similar programs in Java. One inherits from Thread and another implements the interface Runnable :

Program 1:

public class PingPong extends Thread{
private String msg;
private int delay;

PingPong(String s,int tempo){
    msg = s;
    delay = tempo;
}

public void run(){
    try{
        for (int i=1;i<=10;i++){
            System.out.println(msg+ " " + i);
            Thread.sleep(delay);
        }
    }
    catch (Exception e){
        System.err.println("Deu pau!");
        return;
    }
}

public static void main(String[] args) {
    PingPong ping = new PingPong("ping",500);
    ping.start();

    PingPong pong = new PingPong("pong",1000);
    pong.start();

    System.out.println("*** FIM DO PROGRAMA PRINCIPAL ***");
}}

It generates the following output:

Andwehaveprogram2:

publicclassPingPong2implementsRunnable{privateStringmsg;privateintdelay;PingPong2(Strings,inttempo){msg=s;delay=tempo;}publicvoidrun(){try{for(inti=1;i<=10;i++){System.out.println(msg+" " + i);
                Thread.sleep(delay);
            }
        }
        catch (Exception e){
            System.err.println("Deu pau!");
            return;
        }
    }

    public static void main(String[] args) {
        Runnable ping = new PingPong2("ping",500);
        Runnable pong = new PingPong2("pong",1000);

        new Thread(ping).start();
        new Thread(pong).start();
        System.out.println("*** FIM DO PROGRAMA PRINCIPAL ***");
    }
}  

Generating the following output:

Becausetheoutputinprogram1doesnotprint"ping1" because in the implementation of the run() method, on-screen printing is done before putting the thread to sleep )?

    
asked by anonymous 29.05.2016 / 15:41

1 answer

4

First: the class of the first example is conceptually wrong. PingPong is not a Thread , so you should not inherit from it. It works. But it is wrong. But the problem is not that. I understand that this is just a test, but it's common for people to transpose such things into normal code. So worth the alert.

Threads are complicated, unpredictable, the programmer has little control over them. They are two distinct and independent execution lines, so there is no guarantee of order of execution. Threads tend to create race conditions .

Depends on operating system scheduling. Especially the first run can make a big difference because creating a new thread is costly.

What probably occurred is that running pong took a more favorable schedule and was able to complete the creation of the thread and its start earlier. Most likely the thread of the ping started before, but what matters is the completion.

The second example could have happened the same, only gave "luck" not to occur. Each execution will be different. Note that it also does not run as expected and merges ping with pong in the first few runs as if delay were equal. >

Do not use threads if you need predictability. They are less useful than people realize.

29.05.2016 / 16:25