0
class myRunnable implements Runnable {

    public void run() {
        // TODO Auto-generated method stub
        System.out.println("run");
    }

}

public class TestThread {

    public static void main(String[] args) {
        Runnable threadJob = new myRunnable();
        Thread t = new Thread(threadJob);
        t.start();
        for (int i = 0; i < 100000; i++) {
            System.out.println("main");
        }

    }
}

Result in the console:

main main main main ... main main main main

I can't find any "run" word, this means the run method didn't run. Can somebody explain that for me. Thank you.

PS: when i<10, i<100, i<1000, i<10000, I can find the "run" word, but when i<100000 then I can't find the "run" word, that's just weird

enter image description here

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79

5 Answers5

4

Run has been printed out. But your console-buffer is not large enougth.

Change the Console-configuration to unlimited buffer.enter image description here

Grim
  • 1,938
  • 10
  • 56
  • 123
1

First of all, Yes, Your code actually prints "run". Just make this little change below and You´ll see it.

A more rigorous test, If You can see it in a different way, is to send the strings to a text file instead of the console. You will certanly find the word "run".

class myRunnable implements Runnable {

  @Override
  public void run() {
    // TODO Auto-generated method stub
    System.out.println("run");
  }

}

public class TestThread {

  public static void main(String[] args) {
    Runnable threadJob = new myRunnable();
    Thread t = new Thread(threadJob);
    t.start();
    for (int i = 0; i < 100000; i++) {
      System.out.println("main");
      try {
        Thread.sleep(1000);
      }
      catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

  }
}
Cláudio
  • 484
  • 1
  • 4
  • 18
0

Your implementation runs just fine. You just don't see your run message because there hasn't been any thread switch just yet.

The problem is that the action on which you examine the threads is too short. You can insert a Thread.sleep(2000); for example to check it out.

Eypros
  • 5,370
  • 6
  • 42
  • 75
  • I don't think so, probably "run" is printed and he's just missing it. – m0skit0 Jan 26 '15 at 12:16
  • your statement about "thread switch" doesnt make any sense whatsoever. Im guessing you were referring to CONTEXT SWITCHING - which is entirely managed by the VM and has only little influence on THREAD EXECUTION, from a high-level standpoint every thread appears to run simultaneously and concurrently so _output_ to `System.out` will show up nigh-instantly, maybe it will show up on line 3 or 4 (pretty early during runtime), depending on the current system-load. – specializt Jan 26 '15 at 12:16
  • @specializt Well, no, this answer does make sense in fact. The proper definition is actually *"thread context switching"*, so in fact his use is more specific than yours ("context switching" could also refer to "process context switching", so it is highly ambiguous). And no, JVM doesn't switch the threads, the OS does (thread creation are native calls, you can check the source code for Thread class). And yes, it *appears* to run simultaneously but might be or not. – m0skit0 Jan 26 '15 at 12:21
0

I find this wired that you can't find a run in your output, the thing is that you should understand how the java thread mechanism works, the main thread will not wait for the child thread to complete their work, unless you make it specific, thus whether or not the child complete before the main thread complete (and exit) is not expectancy.

if you do want the main thread to wait for the child thread to complete, you can make it specific by:

 t.start();
 t.join();

You should have to catch some exception to make this work.

But I think it should be high ratio that you should see a run printed in your original code. because it seems the main thread is more time consuming. regardless of this, there is nothing to blame if your jvm behave like this. the thread executing order is not insured by the standard anyway.

armnotstrong
  • 8,605
  • 16
  • 65
  • 130
0

Simply add a delay of one second within the loop as displayed below:

class myRunnable implements Runnable {

    public void run() {
        System.out.println("run");
    }

}

public class TestThread {

    public static void main(String[] args) {
        Runnable threadJob = new myRunnable();
        Thread t = new Thread(threadJob);
        t.start();
        for (int i = 0; i < 100000; i++) {
            System.out.println("main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
EduardoFernandes
  • 3,091
  • 1
  • 13
  • 12