0

I need to know, how the execution of this program of mine takes place. I will place my understanding as far as going statement by statement:

The main method gets called and the objects are created. Once the start method of first object is called, it waits for the thread to run. When it runs, it invokes the run method which in turn starts the for loop. At the beginning of the loop i have set Thread.sleep which makes this thread to sleep for 5seconds. After that it again starts and prints along the complete loop. Same when other thread is called.

However the output is reverse and is like {1,1 then 2,2 then 3,3 then 4,4}. Can anyone make understand how this goes about?

package thread;

public class th implements Runnable {
public void run() {
    for (int i = 1; i < 5; i++) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
          System.out.println(i);
    }
}

public static void main(String args[]) {
    th t1 = new th();
    th t2 = new th();
    Thread t = new Thread(t1);
    Thread tn = new Thread(t2);
    t.start();
    tn.start();
}
}
icantcode
  • 167
  • 15
  • 1
    What would you expect the output to be? Why? – Sotirios Delimanolis Oct 02 '15 at 15:43
  • Wait for 5 seconds, since that is the first statement in the loop. The print 1. then increment i to 2. Again wait for 5 seconds, then go again. Once this in finished, then the other Thread (tn) should start as per getting called in the main method @SotiriosDelimanolis – icantcode Oct 02 '15 at 15:44
  • 1
    Threads are, by definition, concurrent. They run in parallel. – Sotirios Delimanolis Oct 02 '15 at 15:45
  • @SotiriosDelimanolis, so can you please provide me an answer with implementation of the same in my program, would help a lot to get basic stuff going right. Thanks much! – icantcode Oct 02 '15 at 15:45
  • Implementation of what? If you want sequential execution, don't use threads. – Sotirios Delimanolis Oct 02 '15 at 15:46
  • Just calling the for loop and it's code directly will do what you are asking. – Philip Whitehouse Oct 02 '15 at 15:46
  • But i read somewhere that only 1 thread can execute inside 1 process. So does that execution of thread concurrently, have to do with different machine. Like a machine having only 1 core would execute only 1 thread and that would be equivalent to sequential? – icantcode Oct 02 '15 at 15:47
  • my concept says "well that at a time only one thread is executed. If you sleep a thread for the specified time,the thread shedular picks up another thread and so on." – icantcode Oct 02 '15 at 15:51
  • either you must have misunderstood, or you have been reading something bogus. even if the machine has only 1 core, the OS scheduler can switch contexts between threads. there is no requirement that a single thread run to completion before the next can have a chance. – Nathan Hughes Oct 02 '15 at 15:52
  • "only 1 thread can execute inside 1 process": this is wrong. It's just not how it works. By design, and by default, multiple threads will execute concurrently. That's exactly what you are observing, isn't it? I'd recommend you study the book "Java Concurrency in Practice" by Brian Goetz. It will give you a better understanding of the subject. – Bruno Reis Oct 02 '15 at 15:56
  • Maybe you're thinking of language implementations (CPython, MRI Ruby) that have a global interpreter lock. Java isn't one of those. – Nathan Hughes Oct 02 '15 at 15:57
  • @all, i have read this and confirmed from a developers website as well. Since each processor can execute only one thread at a time, with multiple processors, multiple threads can execute simultaneously. http://www.qnx.com/developers/docs/6.4.1/neutrino/getting_started/s1_procs.html . Please correct if i have misunderstood – icantcode Oct 02 '15 at 16:03
  • the post by Martijn Pieters in this thread http://stackoverflow.com/questions/200469/what-is-the-difference-between-a-process-and-a-threadalso says the same" Usually, a process has only one thread of control – one set of machine instructions executing at a time." – icantcode Oct 02 '15 at 16:06
  • [that](http://stackoverflow.com/a/2477945/217324) is not Martijn Pieters' post, he just edited it most recently. and the bullet point underneath that one says "A process may also be made up of multiple threads of execution that execute instructions concurrently." – Nathan Hughes Oct 02 '15 at 16:10
  • @NathanHughes so can you explain me the difference between both of these points? the one i i am referring and the one you are – icantcode Oct 02 '15 at 16:11

1 Answers1

0

Both the threads run at the same time. Thread t1 does not run to completion before t2 starts. Instead both t1 and t2 start. The start method returns right way. So it main calls start on t1 then immediately calls start on t2.

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68