ThreadsExecution

ThreadsExecution

    *Below are two classes that i have written to execute thread programs.But output was not as i expected.
My doubt is in output,value of main thread is 2 should be printed first
but it is not printed,instead child thread value is printed first
.Please help me.*


        package corejava.j2se.satya;

        public class Threadstarter implements Runnable 
        {
        Thread t;
        String sample;
        public Threadstarter(String sam) {

            sample = sam;
            t=new Thread(this);
            t.start();
        }

        public void run()
        {

            try
            {
                for(int i=2;i>0;i--)

                {

                    System.out.println("chjild thread is"+" "+sample+" "+ i);
                    t.sleep(250);

                }
            }catch(InterruptedException e)
            {
                System.out.println(e.getMessage());
            }
        }


        }
    ---------------------------------------------------------------------------------------
    package corejava.j2se.satya;

    public class ThreadFinal {

        @SuppressWarnings("unused")
        public static void main(String[] args) {

            Threadstarter sa=new Threadstarter("sa");

            Threadstarter saone=new Threadstarter("saone");
            Threadstarter satwo=new Threadstarter("satwo");

        try
        {
            for(int j=2;j>0;j--)
            {
                System.out.println("value of main thread is"+" "+ j);
    Thread.sleep(1000);
            }
        }catch(Exception e)
        {
        System.out.println(e.getMessage());
        }

        }

    }
-------------------------------------------------------
output is 
-----------
chjild thread is sa 2
chjild thread is saone 2
value of main thread is 2
chjild thread is satwo 2
chjild thread is sa 1
chjild thread is saone 1
chjild thread is satwo 1
value of main thread is 1
View Answers









Related Tutorials/Questions & Answers:
ThreadsExecution

Ads