CS508 ASSISGNMENT 3 SOLUTION FALL 2019 2020

CS508 ASSISGNMENT 3 SOLUTION FALL 2019 2020

 

 

Instruction:

 

Create Ur NEW Project Name Of threads …. Jo b Naam dy java class ka os lehaz se chnge kr  ly code se b  

Launcher >>> code paste in NetBean

/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package threads;

 



 

/**

 *

 * @author Markhor

 */

import java.util.concurrent.ArrayBlockingQueue;

import java.util.concurrent.BlockingQueue;

 

 

public class Launcher {

 

  public static void main(String[] args) {

 

    BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(1024);

 

    ReaderThread reader = new ReaderThread(queue);

    WriterThread writer = new WriterThread(queue);

 

    System.out.println(“My ID is BC00000 and special character to read is ‘$'”);

    new Thread(reader).start();

    new Thread(writer).start();   

   

   

    }

 

 }

 

Create New Java Class with name  ReaderThread …. Jo b Naam dy java class ka os lehaz se chnge kr  ly code se b  

 

ReaderThread >>> code paste in NetBean

 

/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package threads;

 



/**

 *

 * @author Markhor

 */

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.concurrent.BlockingQueue;

 

public class ReaderThread implements Runnable{

 

  protected BlockingQueue<Integer> blockingQueue ;

 

  public ReaderThread(BlockingQueue<Integer> blockingQueue){

    this.blockingQueue = blockingQueue;    

  }

 

  @Override

  public void run() {

     

      System.out.println(“Reader Thread is Started”);

     

    BufferedReader br = null;

     try {

            br = new BufferedReader(new FileReader(new File(“./inputFile.txt”)));

            int buffer =0;

            char match = ‘$’;

           

            while((buffer=br.read())!= -1){

               

                if ((char)buffer == match){

                blockingQueue.put(buffer);

                }

              

            }

           

            blockingQueue.put(0);  //When end of file has been reached

             System.out.println(“Reader Thread ended”);

        } catch (FileNotFoundException e) {

 

            e.printStackTrace();

        } catch (IOException e) {

 

            e.printStackTrace();

        } catch(InterruptedException e){

 

        }finally{

            try {

                br.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

 

 

  }

 

 

 

}

Create New Java Class with name  WriterThread…Jo b Naam dy java class ka os lehaz se chnge kr  ly code se b  

WriterThread >>> code paste in NetBean

 

 

/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package threads;

 

/**

 *

 * @author Markhor

 */

 

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.concurrent.BlockingQueue;

 

public class WriterThread implements Runnable{

 

  protected BlockingQueue<Integer> blockingQueue;

 

  public WriterThread(BlockingQueue<Integer> blockingQueue){

    this.blockingQueue = blockingQueue;    

  }

 

  @Override

  public void run() {

     

      System.out.println(“Writer Thread is Started”);

     

    PrintWriter writer = null;

    char match;

    int no = 0;

    try {

        writer = new PrintWriter(new File(“outputFile.txt”));

 

        while(true){

            int buffer = blockingQueue.take();

           

            match = (char)buffer;

            no++;

           

            if(buffer == (0)){

                break;

            }

           

            writer.println(no);

            System.out.println(“‘”+match+”‘” + ” Found so Far :” + no);

           

        }              

        System.out.println(“Writer Thread ended”);

        System.out.println(“Total number of Special character “+”‘$'”+” in the file are :”+ (no-1));

    } catch (FileNotFoundException e) {

 

        e.printStackTrace();

    } catch(InterruptedException e){

 

    }finally{

        writer.close();

    }

 

  }

 

 

}

 

You May Also Like

Leave a Reply

%d bloggers like this: