//Assignment No:2
Set A
a) Program to define a thread for printing text on output screen for ‘n’ number of times. Create 3 threads and run them. Pass the text ‘n’ parameters to the thread constructor. Example:
i. First thread prints “COVID19” 10 times.
ii. Second thread prints “LOCKDOWN2020” 20 times
iii. Third thread prints “VACCINATED2021” 30 times
CODE
š
- import java.util.*;
- class Mythread extends Thread
- {
- String msg;
- int n;
- Mythread(String m,int a)
- {
- msg=m;
- n=a;
- }
- public void run()
- {
- for(int i=0; i<n; i++)
- System.out.println(msg+ " "+i);
- }
- }
- public class disease
- {
- public static void main(String[] args)
- {
- Mythread m1=new Mythread("COVID19",10) ;
- Mythread m2=new Mythread("LOCKDOWN2020",20) ;
- Mythread m3=new Mythread("VACCINATED2021",30) ;
- m1.start();
- m2.start();
- m3.start();
- }
- }
b) Write a program in which thread sleep for 6 sec in the loop in reverse order from 100
to 1 and change the name of thread.
CODE
š
- import java.io.*;
- class reverse_thread
- {
- public static void main(String[] args)
- {
- Thread t=Thread.currentThread();
- System.out.println("Current Thread is: "+t);
- t.setName("My Thread");
- System.out.println("After changing the name thread is:" +t);
- try
- {
- for(int n=10; n>0;n--)
- {
- System.out.println(n);
- Thread.sleep(6000);
- }
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- }
c) Write a program to solve producer consumer problem in which a producer produces a
value and consumer consume the value before producer generate the next value.
(Hint: use thread synchronization)
CODE
š
- import java.io.*;
- import java.lang.*;
- class Shared
- {
- int a;
- boolean valueChanged=false;
- synchronized int get_data()
- {
- if(!valueChanged)
- try
- {
- wait();
- }
- catch(InterruptedException e)
- {
- System.out.println("Interrupted");
- }
- System.out.println("Read:" +a);
- valueChanged=false;
- notify();
- return a;
- }
- synchronized void put_data(int n)
- {
- if(valueChanged)
- try
- {
- wait();
- }
- catch(InterruptedException e)
- {
- System.out.println("Interrupted");
- }
- this.a=n;
- valueChanged=true;
- System.out.println("Written: "+a);
- notify();
- }
- }
- class Producer implements Runnable
- {
- Shared ob;
- Producer(Shared ob)
- {
- this.ob=ob;
- new Thread(this,"Producer").start();
- }
- public void run()
- {
- int j=0;
- while(true)
- {
- ob.put_data(j++);
- }
- }
- }
- class Consumer implements Runnable
- {
- Shared ob;
- Consumer(Shared ob)
- {
- this.ob=ob;
- new Thread(this,"Consumer").start();
- }
- public void run()
- {
- while(true)
- {
- ob.get_data();
- }
- }
- }
- class sync
- {
- public static void main(String args[])throws IOException
- {
- Shared ob=new Shared();
- new Producer(ob);
- new Consumer(ob);
- System.out.println("Press Ctrl+c to stop");
- }
- }
Set B
a) Write a program to calculate the sum and average of an array of 1000 integers
(generated randomly) using 10 threads. Each thread calculates the sum of 100
integers. Use these values to calculate average. [Use join method ].
CODE
š
- import java.util.*;
- class thread implements Runnable
- {
- Thread t;
- int i,no,sum;
- int a[]=new int[1000];
- thread(String s,int n)
- {
- Random rs=new Random();
- t=new Thread(this,s);
- n=no;
- int j=0;
- for(i=0; i<1000; i++)
- {
- a[j]=rs.nextInt()%100;
- j++;
- }
- t.start();
- }
- public void run()
- {
- for(i=0; i<1000; i++)
- {
- sum=sum+a[no];
- no++;
- }
- System.out.println("Sum =" +sum);
- System.out.println("Avg =" +sum/100);
- }
- }
- public class calc
- {
- public static void main(String[] args)throws Exception
- {
- thread t1=new thread("a",0);
- t1.t.join();
- thread t2=new thread("b",100);
- t2.t.join();
- thread t3=new thread("c",200);
- t3.t.join();
- thread t4=new thread("d",300);
- t4.t.join();
- thread t5=new thread("e",400);
- t5.t.join();
- thread t6=new thread("f",500);
- t6.t.join();
- thread t7=new thread("g",600);
- t7.t.join();
- thread t8=new thread("i",700);
- t8.t.join();
- thread t9=new thread("j",800);
- t9.t.join();
- thread t10=new thread("k",900);
- t10.t.join();
- }
- }
b) Write a program for a simple search engine. Accept a string to be searched. Search for
the string in all text files in the current folder. Use a separate thread for each file. The
result should display the filename, line number where the string is found.
CODE
š
- import java.io.*;
- import java.util.*;
- public class search_engine extends Thread
- {
- File f1;
- String fname ,line;
- static String str;
- LineNumberReader r=null;
- search_engine(String fname)
- {
- this.fname=fname;
- f1=new File(fname);
- }
- public void run()
- {
- try
- {
- FileReader fr=new FileReader(f1);
- r=new LineNumberReader(fr);
- while((line=r.readLine())!=null)
- {
- if(line.indexOf(str)!=-1)
- {
- System.out.println("String found in "+fname+ " at "+r.getLineNumber()+ " line");
- stop();
- }
- }
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- public static void main(String[] args)throws IOException
- {
- Thread t[]=new Thread[20];
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Enter String to be Search :");
- str=br.readLine();
- FilenameFilter f=new FilenameFilter()
- {
- public boolean accept(File file, String name)
- {
- if(name.endsWith(".txt"))
- return true;
- else
- return false;
- }
- };
- File dir1=new File("."); //current directory
- File[] files=dir1.listFiles(f);
- if(files.length==0)
- System.out.println("no files available with this extension ");
- else
- {
- for(int i=0; i<files.length; i++)
- {
- for(File af:files)
- {
- t[i]=new search_engine(af.getName());
- t[i].start();
- }
- }
- }
- }
- }
c) Write a program that implements a multi-thread application that has three threads.
First thread generates random integer every 1 second and if the value is even, second
thread computes the square of the number and prints. If the value is odd, the third
thread will print the value of cube of the number
CODE
š