-2

I have a simple test with thread:

package Thread;

class Sum extends Thread {
    int low, up, S;

    public Sum(int a, int b) {
        low = a;
        up = b;
        S = 0;
        System.out.println("This is Thread " + this.getId());
    }

    @Override
    public void run() {
        for (int i = low; i < up; i++) {
            S += i;
        }
        System.out.println(this.getId() + ":" + S);
    }
}

public class Tester {
    public static void main(String agrs[]) {
        Sum T1 = new Sum(1, 100);
        T1.start();
        Sum T2 = new Sum(10, 100);
        T2.start();
        System.out.println("Main process terminated");
    }
}

but i don't understand when was run() method executed, it return same that:

This is Thread 8
This is Thread 9
Main process terminated
9:4905
8:4950

It's mean the run() method was executed after T1 and T2 start. I still think that when start() method was invoked the run() will be execute. Thank for advance!

Shashi
  • 12,487
  • 17
  • 65
  • 111
Thangnv
  • 805
  • 3
  • 11
  • 22

4 Answers4

8

When you call start() on the thread object , it invokes the run() method.

When in doubt , read the documentation: Thread#start():

causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

Suggested Reading:

  1. Why we call Thread.start() method which in turns calls run method?
  2. Oracle tutorial
  3. Why is run() not immediately called when start() called on a thread object in java
Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • thanks! but i don't know why statement System.out.println("Main process terminated"); was executed before my run() execute ? What different between run() i was overried with run() of this thread. thanks! – Thangnv Jul 25 '13 at 07:30
  • @Thangnv Threads they are , the way the code is now , sequencing cannot be guaranteed ! – AllTooSir Jul 25 '13 at 07:31
  • why? I really want to ask this problem! – Thangnv Jul 25 '13 at 07:34
  • my god, everyone don't understand my question, so vote down :(. Thanks for advances – Thangnv Jul 25 '13 at 07:37
  • 1
    @Thangnv Read the documents and answers I linked to my post . You will understand . – AllTooSir Jul 25 '13 at 07:38
1

The start() method creates a new thread, that executes the run() method.

From Documentation:

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Maroun
  • 94,125
  • 30
  • 188
  • 241
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
1

When we invoke Thread.start JVM creates a new native thread and then calls Thread.run which causes this Thread to begin execution. Thread.run is invoked by JVM asynchroneously, so these 3 lines

Main process terminated
9:4905
8:4950

might appear in any order.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

The thread scheduler is selecting which thread for run. It can be random selection if you are not giving any priority for the thraed. Here the thread scheduler will select thread and it will work in two separate stack space as two independent thread process. So thread T1 starts before thread T2 and there is no sleep, no wait, no join the both will give output at same time.

for more details please visit http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start%28%29

you can understand it more clearly if you use remaining functions of Thread like sleep(), join(), wait() etc...

Niju
  • 487
  • 1
  • 9
  • 18