Tuesday, 23 January 2018

Write a simple java application that creates two threads: One thread creates even numbers and another thread creates odd numbers.




Program:-
class Threaddemo implements Runnable
{
            Thread t;
            Threaddemo(String name)
            {
                        t = new Thread(this,name);
                        System.out.println("Curent Thread: "+t);
                        t.start();
                       
            }
           
            public void run()
            {
                        try
                        {
                                    for(int i=0;i<10;i++)
                                    {
                                                if(i%2!=0)
                                                {
                                                            System.out.println("Odd Thread: "+ i);
                                                            Thread.sleep(500);
                                                }
                                    }
                        }catch(Exception e)
                        {
                                    System.out.println("Thread Interrupted... "+e);
                        }
                       
                        System.out.println("Child thread exiting.");
            }
}

class u71
{
            public static void main(String ar[])
            {
                        new Threaddemo("Number");
                       
                        Thread t1 = Thread.currentThread();
                        System.out.println("Current thread: " + t1);
                        try
                        {
                                    for(int i=0;i<10;i++)
                                    {
                                                if(i%2==0)
                                                {
                                                            System.out.println("Even Thread: "+ i);
                                                            Thread.sleep(500);
                                                }
                                    }
                        }catch(Exception e)
                        {
                                    System.out.println("Thread Interrupted... "+e);
                        }
            System.out.println("Main thread exiting.");

            }
}

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...