bimal chawla
bimal chawla

Reputation: 449

runnable interface example

public class CreateThreadRunnableExample implements Runnable {

    public void run() {

        for (int i = 0; i < 5; i++) {
            System.out.println("Child Thread : " + i);

            try {
                Thread.sleep(50);
            } catch (InterruptedException ie) {
                System.out.println("Child thread interrupted! " + ie);
            }
        }

        System.out.println("Child thread finished!");
    }

    public static void main(String[] args) {

        Thread t = new Thread(new CreateThreadRunnableExample(), "My Thread");

        t.start();

        for (int i = 0; i < 5; i++) {

            System.out.println("Main thread : " + i);

            try {
                Thread.sleep(100);
            } catch (InterruptedException ie) {
                System.out.println("Child thread interrupted! " + ie);
            }
        }
        System.out.println("Main thread finished!");
    }
}

in this program two sleep methods are used of deifferent time..,,, so if main thread run time then child thread have to run 2 time.but it runs one time only....it we take the concept of runnable or running state....then when main thread end,then 2 child threads will be in ready state then why only one child thread runs.

Upvotes: 11

Views: 64675

Answers (4)

MAnoj Sarnaik
MAnoj Sarnaik

Reputation: 1650

TRY THIS

  public class Test implements Runnable{

        public void run() {

            for (int i = 0; i < 5; i++) {
                System.out.println("CHILD THREAD : " + i);

                try {
                    Thread.sleep(50);
                } catch (InterruptedException ie) {
                    System.out.println("CHILD THREAD INTERRUPTED! " + ie);
                }
            }

            System.out.println("CHILD THREAD FINISHED!");
        }

        public static void main(String[] args) {

            Thread t = new Thread(new Test());

            t.start();

            for (int i = 0; i < 5; i++) {
                System.out.println("MAIN THREAD : " + i);
                try {
                    Thread.sleep(200);
                } catch (InterruptedException ie) {
                    System.out.println("CHILD THREAD INTERRUPTED! " + ie);
                }
            }
            System.out.println("MAIN THREAD FINISHED!");
        }
    }

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

First of all you have added System.out.println("Child thread interrupted! " + ie); for both the main and the child thread, thats a typo...

Try this... Both the threads are running (Main and the Child thread)

Main method of this program is put on to the bottom of the Main thread created by JVM, and the Main method creates another Runtime Stack and puts the child thread in it.

public class DemoThread implements Runnable {

    public void run() {

        for (int i = 0; i < 3; i++) {
            System.out.println("Child Thread ");

            try {
                Thread.sleep(200);
            } catch (InterruptedException ie) {
                System.out.println("Child thread interrupted! " + ie);
            }
        }

        System.out.println("Child thread finished!");
    }

    public static void main(String[] args) {

        Thread t = new Thread(new DemoThread ());

        t.start();

        for (int i = 0; i < 3; i++) {

            System.out.println("Main thread);

            try {
                Thread.sleep(200);
            } catch (InterruptedException ie) {
                System.out.println("Child thread interrupted! " + ie);
            }
        }
        System.out.println("Main thread finished!");
    }
}

Upvotes: 3

npinti
npinti

Reputation: 52205

The sleep method is not 100% time accurate, as stated in the JavaDoc:

public static void sleep(long millis) throws InterruptedException

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.

So basically the code might sometimes work as expected and the other times not.

Also, you seem to be starting only 1 child, so why are you expecting 2?

EDIT: In theory yes, the child thread should execute twice while the main thread is asleep. That being said, as explained in the JavaDoc, the accuracy with which the sleep method executes depends on the system upon which it is running, so at times you might get it to work as expected and at other times not. So for instance, you could have scenarios where the main thread sleeps for, say for instance, 97 milliseconds and not 100, while the child, sleeps for, say, 53 milliseconds. This will cause the child to execute only once.

Another option would be to do something like so: while (currentTime <= timeNeededToElapse) { currentTime=... }. This causes a tight while loop which could offer better control but it is still, to my knowledge not 100% accurate not to mention that you will be consuming CPU cycles for effectively doing nothing, so it is to be used with caution.

Upvotes: 2

P&#233;ter T&#246;r&#246;k
P&#233;ter T&#246;r&#246;k

Reputation: 116306

You are starting only one child thread so why are you expecting two?

About the timeouts, sleep time is not exact, so while one thread sleeps for 100 ms, another may or may not sleep twice for 50 ms. This is a classic example of a race condition. If you run the example code several times, you may see two child thread messages in some cases, and one in other cases.

Upvotes: 1

Related Questions