Barriers with threads - Output code excerpt

1

I have the following code snippet. Can someone explain the following question to me?

In main when I create the two threads, I go to the Boat constructor and notice that it sleeps a second. Why is it main that assumes this sleep in its time and why is not each thread individually?

public class Boat extends Thread{
private Dock dock;
private long initialTime;

public Boat(String name, Dock dock, long initialTime) throws InterruptedException{
    setName(name);
    this.initialTime = initialTime;
    this.dock = dock;
    sleep(1000);
}

@Override
public void run(){
    dock.enter();
    try{
        dock.exit();
        sleep(1000);
        System.out.println(getName() + "-" + (System.currentTimeMillis()-initialTime)/1000 + "s");

    }catch(InterruptedException e){

    }
}

public static void main(String[] args) throws InterruptedException{
    long sTime = System.currentTimeMillis();
    Dock d = new Dock();
    Boat t1 = new Boat("b1", d, sTime);
    Boat t2 = new Boat("b2", d, sTime);
    System.out.println("Start!");
    t1.start();
    sleep(1000);
    t1.interrupt();
    t2.start();
    sleep(1000);
    d.exit();
    t2.join();
    sleep(1000);
    System.out.println("Main - Finished " + (System.currentTimeMillis()-sTime)/1000+"s");
}
}
    
asked by anonymous 04.01.2017 / 01:18

1 answer

2

There are three threads in your program, the main, b1 and b2 .

The main thread starts the program by creating the other two. Note that the main thread invokes the constructor for the b1 object. The fact that b1 inherits from Thread is irrelevant until this point, this will only make a difference when start() is called. So far, b1 is just an object like any other. Therefore, who will execute the sleep(1000) of the b1 constructor is the main thread. The same happens with b2 .

Only after these two sleep s of constructors will Start! appear on the console and it will start the b1 thread. The b1 thread will execute its run() method.

The interrupt() method asks for another thread to stop, but does not expect it to actually stop. The join() method, however, waits for the other thread to stop, but does not prompt it to stop. To do both, you would have to call each thread interrupt() followed by join() . It happens that in b1 you only call interrupt() and in b2 you only call join() . So the main thread will ask the b1 to stop, but it will not wait for it to stop, whereas it will not ask the b2 to stop but will wait for it to stop. After that all, the main thread will make a sleep(1000) after waiting for the end of b2 , but without ensuring that b1 is finished.

This code demonstrates that you should not be understanding how the flow works when there are multiple threads. Basically, it is the following:

  • The thread that starts executing a method is always the same as the thread ends. There is nothing in the Java language that allows a thread to start executing one method and another to terminate it. This means that everything within main is executed by the main thread.

  • All method calls and constructors that are inside another method executed by a given thread will also be executed by that same thread. This means that the thread executing the main method, invoking the Boat constructor, will itself execute this constructor, since everything within the main method will be executed by the main thread.

  • A thread is actually started only when the start() method is invoked, and the execution of the new thread starts in the corresponding run() method. This means that the thread running the constructor is not the same as the thread that is running the run() . This also means that before start() is invoked, the object representing the thread is just a Java object like any other without anything special.

  • Note that since start() and run() are different methods, rule 2 above is not violated. The main thread executes start() , but the one that executes run() is the created thread.

  • 04.01.2017 / 01:32