Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Little confused on the thread behaviour

I have written a basic code in Thread and the output which i got is pretty surprising.

public class ThreadImp implements Runnable{

    public static void main(String[] args) {
        ThreadImp threadImp = new ThreadImp();
        Thread t =new Thread(threadImp);
        t.setName("Fred");
        t.start();
        threadImp.run();
        t.run();
    
    }
    public void run(){
        System.out.println("Current Thread: "+ Thread.currentThread());
    }
}

The output here i am expecting is that it will be print

Current Thread: Thread[main,5,main]

Current Thread: Thread[Fred,5,main]

Current Thread: Thread[main,5,main]

This result i can understand, that there is only one thread which i am creating i.e Thread Fred. Other two calls to run method is just like calling a normal method.

But what i can't understand is sometimes when i execute the same program i am getting the following output. So can anyone explain it to me why is it behaving like this.

Current Thread: Thread[main,5,main]

Current Thread: Thread[Fred,5,main]

like image 432
Jean Avatar asked Mar 16 '16 15:03

Jean


People also ask

What will happen if multiple threads accessing the same resource?

Multiple threads accessing shared data simultaneously may lead to a timing dependent error known as data race condition. Data races may be hidden in the code without interfering or harming the program execution until the moment when threads are scheduled in a scenario (the condition) that break the program execution.

What happens when a thread executes wait () method on an object without owning the object's lock?

For a thread to call wait() or notify(), the thread has to be the owner of the lock for that object. Otherwise, a runtime error occur and the rest of code is not executed.

What happens when the JVM encounters a wait () call?

wait() causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). The current thread must own this object's monitor.

What happens when a thread occupies a resource for long time so that the other thread keeps on waiting for indefinite time?

Threads waiting for each other to terminate is the deadlock situation. It is the situation that occurs when we use multiple threads in our program. Deadlock results in no progress of the threads involved in a deadlock situation. Threads are in a wait state for an infinite amount of time.


1 Answers

You call Thread.run() (which you normally shouldn't call directly), and that does nothing if the Thread has completed and cleaned itself up:

@Override
public void run() {
    if (target != null) {
        target.run();
    }
}

Source: Java 7 update 79

Where target is the provided runnable. The field target is set to null when the thread has ended:

/**
 * This method is called by the system to give a Thread
 * a chance to clean up before it actually exits.
 */
private void exit() {
    if (group != null) {
        group.threadTerminated(this);
        group = null;
    }
    /* Aggressively null out all reference fields: see bug 4006245 */
    target = null;
    /* Speed the release of some of these resources */
    threadLocals = null;
    inheritableThreadLocals = null;
    inheritedAccessControlContext = null;
    blocker = null;
    uncaughtExceptionHandler = null;
}

Source: Java 7 update 79

So the exact output (including order, and if you get two or three messages) depends on timing, scheduling, etc.

like image 107
Mark Rotteveel Avatar answered Sep 22 '22 17:09

Mark Rotteveel