1

Hi i am studying and playing with thread in java. i read in a book that Thread object and Running Thread is not same thing.even the thread complete it's run method running thread goes into dead state i even check that with isAlive() method. i want to know that if both are different then the following code is not working as per i understand.

public class Main {

    public static void main(String[] args) throws ParseException {
        Student s = new Student();

        Thread t = new Thread(s);

        t.start();
        t.run();
        t.run();
        t.run();

        t.run();
        t.run();

    }
}

class Student implements Runnable {
    public void run() {

        try {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

it only shows this output. main Thread-0 main or this Thread-0 main

from this result i understand that after thread complete it's run method. Running thread goes into dead State and calling on Thread obj method no working.but i couldn't understand the reason behind because Thread object is skill reference and what about the other methods of Thread class. like yield()? start()?

here is another scenario for clear understanding what i said

public class Main {

    public static void main(String[] args) throws ParseException {
        Student s = new Student();

        Thread t = new Thread(s);

        t.start();

        if (!t.isAlive()) {

            t.start();
        }

    }
}

class Student implements Runnable {
    public void run() {

        try {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

documentation said if we call start method on Thread t object then it will throw java.lang.IllegalThreadStateException. but the above code working fine. i am very confused about which methods of Thread class rely on Running thread and which for thread object. i hope you understand the problem. thanks in advance?

T.Malik
  • 107
  • 1
  • 10

5 Answers5

2

You can do it on both ways. It's pretty much the same. You should start the thread in your first code file just with a simple

t.start();

I'd remove all the t.run() from your above code, because you are creating a new Thread object with your implemented inner class.

Norukh
  • 364
  • 1
  • 14
  • first thing implementing inner class Student class is separate class. secondly i call the run method many time to know the difference why this method not calling in because it is just dam simple object residing on heap and it still referring to and object what is behind the scene story. – T.Malik Apr 23 '15 at 06:45
2

Right after starting the thread by t.start(), this condition:

if (!t.isAlive()) 

is veeeeeeeeeeeeeeeeeeery unlikely to be met--because started thread doesn't block. That is why it just skips (because t.isAlive() == true) and goes further without an exception.

Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
  • i know most of time it wouldn't happen.but why run and other method still not working as my first code – T.Malik Apr 23 '15 at 06:49
  • because `t` already consumed `Runnable s`, with which it was created, in the first call of `t.start()`. – Alex Salauyou Apr 23 '15 at 06:52
  • "If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; *otherwise, this method does nothing and returns.*"--https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#run%28%29 – Alex Salauyou Apr 23 '15 at 06:54
  • can you elaborate this please – T.Malik Apr 23 '15 at 07:07
2

In your first attempt you never restarted the thread:

t.start();
t.run();// does not restarts the thread, it simply makes synchronous call the run(), hence you don't get the exception
t.start();// add this line, to restart the thread and get the exception

On second attempt, the condition fails as the thread is likely to start and is alive, as per your condition thread must not be alive and it fails to restart the thread.

t.start();
t.join();// add this line, it allows thread to complete first
if (!t.isAlive()) {
    t.start();
}

P.S.

In order to start a thread make call to start() which will cause async call to run(). If you make call to run(), it won't start as thread, it will be synchronous call like normal method invocation.

  • ok Arvind i understand it but i didn't understand yet why it happen.because i run it with extends Thread class of starting thread i called run method so many time and it works. – T.Malik Apr 23 '15 at 07:00
  • @artaxerxe, kindly read the OP post carefully, OP is asking why there are no runtime errors? –  Apr 23 '15 at 08:10
  • @Arvind I think the *runtime error* refers to second post. But anyway, your updated comment clarifies the issue. Thanks :). – artaxerxe Apr 23 '15 at 08:29
1

In the first example you provided, the cause for the program not displaying a count of thread names equal to your t.start() + t.run() calls is the fact that after a thread is dead, you cannot call on it start() or run() again. It's dead. The reason there are 3 outputs is likely because until t.start() enters in dead state, the other 2 calls manage to perform.

In the second example, you should be aware that when a start() is called, the thread state is put on alive. Anyway, in a concurrent environment you cannot rely on operations call sequence if synchronized isn't involved, but, from the result you get, it seems that t.start() is called before the t.isAlive() check. Hope to help.

artaxerxe
  • 6,281
  • 21
  • 68
  • 106
  • how about if i extends from thread class after the thread has been dead.it's run method working – T.Malik Apr 23 '15 at 08:05
  • @T.Malik You cannot extend from an object. You extend from a class. Please be more precisely if I don't understand right. – artaxerxe Apr 23 '15 at 08:25
1

i read in a book that Thread object and Running Thread is not same thing.

Right, a "thread" is an execution of your code. A Thread is a Java object that you can use to create and mananage the life-cycle of a "thread". The "thread" is not created until you call the Thread object's .start() method, and the Thread object can continue to exist even after the "thread" has finished its work and disappeared.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57