Wednesday, 10 September 2014

Multithreading In Java!

//Program for Multiple Threading
class ThreadDemo extends Thread
{
    private Thread t;
    private String thread_name;

    ThreadDemo(String thread_name)
    {
       this.thread_name = thread_name;
       System.out.print("\nCreating "+thread_name);
    }

    public void run()
    {
       System.out.print("\nRunning "+thread_name);

       for(int i=1;i<=9;i++)
       {
            System.out.print("\nSequence Number Series: "+i);
            try
            {
                Thread.sleep(50);
            }
            catch(Exception e)
            {

            }
       }

       System.out.print("\nTerminating "+thread_name);
    }

    public void start()
    {
        System.out.print("\nStarting: "+thread_name);
        if(t == null)
        {
            t = new Thread(this,thread_name);
            t.start();
        }
    }
}

class RunnableDemo implements Runnable
{
    private Thread t1;
    private String thread_name;

    RunnableDemo(String thread_name)
    {
      this.thread_name = thread_name;
      System.out.print("\nCreating "+thread_name);
    }

    public int fibo(int i)
    {
      if(i == 0)
        return 0;
      else if(i == 1)
        return 1;
      else
         return (fibo(i-1)+fibo(i-2));
   }

    public void run()
    {
        System.out.print("\nRunning "+thread_name);

        int i;
        for(i=0;i<=7;i++)
        {
            System.out.print("\nFibonacci Series: "+fibo(i));
            try
            {
                Thread.sleep(100);
            }
            catch(Exception e)
            {

            }
        }

        System.out.println("\nTerminating "+thread_name);
    }

    public void start()
    {
        System.out.print("\nStarting "+thread_name);

        if(t1 == null)
        {
            t1 = new Thread(this,thread_name);
            t1.start();
        }
    }
}

public class TestThread1
{
    public static void main(String s[])
    {
        ThreadDemo thread = new ThreadDemo("Thread-1");
        thread.start();

        RunnableDemo r1 = new RunnableDemo("Thread-2");
        r1.start();
    }
}

//Program for Synchronize Method
class Syncro
{
    synchronized public void print(String s)
    {
      try
      {
         System.out.print("\nHello "+s);
         System.out.print("\n-How Are you???");
         Thread.sleep(100);
      }
      catch(Exception e)
      {

      }
    }
}

class Sample implements Runnable
{
    Syncro syn;
    String s;
    Sample(Syncro syn,String s)
    {
       this.syn = syn;
       this.s = s;
       Thread t = new Thread(this,s);
       t.start();
    }

    public void run()
    {
       synchronized(syn)
       {
          syn.print(s);
       }

       System.out.println();
    }
}

class Synchronize_Demo
{
    public static void main(String s[]) throws Exception
    {
       Syncro syn = new Syncro();

       Sample s1 = new Sample(syn,"ABC");
       Sample s2 = new Sample(syn,"XYZ");
    }
}

1 comment:

  1. The programs are amazing! Thank you again for such a valuable contribution.

    ReplyDelete