Sunday, February 12, 2012

IMPLEMENTATION OF MULTITHREADED PROGRAMMING [ java ]


IMPLEMENTATION OF MULTITHREADED PROGRAMMING


AIM:
            To write the multithreaded programming to print all the prime numbers, fibonacci series and also the numbers common in both prime numbers and fibonacci series below 10

ALGORITHM:
            1.Extend the class MyThread1 from threads. Define the function run() as public
            2.Extend the class MyThread2 from threads and define its data members and functions
            3.In main function ,create objects for piped reader and piped writer
            4.Print all the prime numbers and close the piped reader1
            5.Display the fibonacci series and close the piped reader2

PROGRAM:

import java.io.*;
         import java.util.*;
         class MyThread1 extends Thread
            {
              private PipedReader pr;
              private PipedWriter pw;
              MyThread1(PipedReader pr, PipedWriter pw)
                 {
                      this.pr = pr;
                      this.pw = pw;
                 }
              public void run()
                {
                    try
                     {
                         int i;
                         for (i=1;i<=10;i++)
                           {
                               int j;
                               for (j=2; j<i; j++)
                                {
                                    int n = i%j;
                                    if (n==0)
                                     {
                                         break;
                                     }
                                }
                                  if(i == j)
                                    {
                                      pw.write(i+"\n");
                                    }
                           }
                     pw.close();
                  }
                     catch (IOException e)
                      {
                      }
               }
       }
class MyThread2 extends Thread
{
      private PipedReader pr;
      private PipedWriter pw;
      MyThread2(PipedReader pr, PipedWriter pw)
      {
         this.pr = pr;
         this.pw = pw;
      }
      public void run()
      {
          try
          {
              int f1, f2 = 1, f3 = 1;
              for (int i = 1; i <=10; i++)
              {
                   pw.write(f3+"\n");
                   f1 = f2;
                   f2 = f3;
                   f3 = f1 + f2;
              }
              pw.close();
          }
          catch (IOException e)
          {
          }
      }
}
class MultithreadedProgram
{
public static void main(String[] args) throws Exception
{
    ArrayList list1=new ArrayList();
    ArrayList list2=new ArrayList();
    PipedWriter pw1 = new PipedWriter();
    PipedReader pr1 = new PipedReader(pw1);
    MyThread1 mt1 = new MyThread1(pr1, pw1);
    System.out.println("Prime Numbers: ");
    mt1.start();
    int item1;
    while ((item1 = pr1.read()) != -1){
    char ch1=((char) item1);
    System.out.print(Character.toString(ch1));
    list1.add(Character.toString(ch1));
}
   
    pr1.close();
    PipedWriter pw2 = new PipedWriter();
    PipedReader pr2 = new PipedReader(pw2);
    MyThread2 mt2 = new MyThread2(pr2, pw2);
    System.out.println("Fibonacci Numbers: ");
    mt2.start();
    int item2;
    while ((item2 = pr2.read()) != -1){
    char ch2=((char) item2);
    System.out.print(Character.toString(ch2));
    list2.add(Character.toString(ch2));
  }
   pr2.close();
   System.out.println("Elements common to both lists are:");
   list1.retainAll(list2);
   for(int i=0;i<list1.size();i++)
         {
    System.out.print(list1.get(i));
    }
 }
}


















OUTPUT
Prime numbers
2
3
5
7
Fibonacci Numbers
1
2
3
5
8
Elements common to both list are
2
3
5





















RESULT
            Thus the multithreaded program to display fibonacci series and prime numbers was done   and verified

No comments:

Post a Comment