1

i am new to multithreading and trying to clear my basics.

public class SleepExample extends Thread {

private int counter = 0;

@Override
public void run() {

    try {
        counter++;
        System.out.println("Value of counter "+counter);
        System.out.println("Thread going in sleep "+Thread.currentThread().getName());
        Thread.currentThread().run();
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("Thread out of sleep "+Thread.currentThread().getName());
}

public static void main(String[] args) {
    new SleepExample().start();
    new SleepExample().start();
    Test test = new  Test();
    Thread t = new Thread(test);
    t.start();
}
}

//another class implementing runnable

public class Test implements Runnable {

@Override
public void run() {
    System.out.println("In Test runnable method");

}

}

When i run this code, my run method of SleepExample recursively call itself after below line

Thread.currentThread().run();

for thread belonging to SleepExample (Thread -0, Thread -1) and it goes to run method of Test class for thread t.

I am unable to understand the usage of Thread.currentThread().run();

P.S. - I read its java doc and so i have implemented a runnable

Narendra Pandey
  • 514
  • 4
  • 11
  • 26
  • 1
    Calling `Thread.currentThread().run();` unconditionally seems like a very bad idea. `Thread.run()` is just a regular method, so this will result in a `StackOverflowError`. Even calling it conditionally seems highly questionable - I can't see why you'd want to do this. What are you actually trying to do? – Andy Turner Apr 24 '16 at 16:47
  • Thanks for reply.I was just trying to implement and learn.Yes it did cause exception due to recursive call to run method. – Narendra Pandey Apr 24 '16 at 17:21
  • @Narendra Pandey : Refer to this question for more details on start() and run() methods: http://stackoverflow.com/questions/8052522/why-we-call-thread-start-method-which-in-turns-calls-run-method/36812010#36812010 – Ravindra babu Apr 24 '16 at 17:53

1 Answers1

1

I am unable to understand the usage of Thread.currentThread().run();

You are not supposed to call it directly. From Thread.start() You are supposed to use start() to call run() and that is it.

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.

You are already running in the run() so you should only call this if you can say why you are doing it, even then it will look like a bug or be plain confusing and I would suggest you use a loop instead.

When i run this code, my run method of SleepExample recursively call itself after below line

You have a method calling itself, so you should expect that to happen. There is nothing special to Thread in this regard. It is like any other recursive call in a method.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130