Exercise 7

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Exercise – 7 (Threads)

a) Aim: Write a JAVA program that creates threads by extending Thread class .First
thread display ―Good Morning ―every 1 sec, the second thread displays ―Hello ―every
2 seconds and the third display ―Welcome‖ every 3 seconds ,(Repeat the same by
implementing Runnable)
b) Write a program illustrating isAlive and join ()
c) Write a Program illustrating Daemon Threads.

Description:

Multithreading in Java is a process of executing multiple threads simultaneously.

A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and


multithreading, both are used to achieve multitasking.

Program:
import java.io.*;
class One extends Thread
{
public void run()
{
for(int i=0;i<100;i++)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println("Good Morning");
}
}
}
class Two extends Thread
{
public void run()
{

for(int i=0;i<100;i++)
{
try {
}
Thread.sleep(2000);
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println("Hello ");
}
}
}
class Three implements Runnable
{
public void run()
{
for(int i=0;i<100;i++)
{
try
{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println("Wel come");
}

}
}

class ThreadEx
{
public static void main(String[] args)
{
One t1=new One();
Two t2=new Two();
Three tt=new Three();
Thread t3=new Thread(tt);
t1.setName("One");
t2.setName("Two");
t3.setName("Three");
System.out.println(t1);
System.out.println(t2);
System.out.println(t3);
Thread t=Thread.currentThread();
System.out.println(t);
t1.start();t2.start();t3.start();
}
}

b) Aim: Write a program illustrating isAlive and join ()

Descriptoin:
The isAlive() method of thread class tests if the thread is alive. A thread is considered alive
when the start() method of thread class has been called and the thread is not yet dead. This
method returns true if the thread is still running and not finished.
Program:

class NewThread implements Runnable

{
String name;
NewThread(String threadname)
{
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}

}
class DemoJoin
{
public static void main(String args[])
{
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new
NewThread("Three");
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
// wait for threads to finish
try
{
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch (InterruptedException e)
{
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
}
c) Aim: Write a Program illustrating Daemon Threads.

Programs:
public class TestDaemonThread1 extends Thread
{
public void run()
{
if(Thread.currentThread().isDaemon())
{//checking for daemon thread
System.out.println("daemon thread work");
}

System.out.println("user thread work");

else
{

}
}
public static void main(String[] args)
{
TestDaemonThread1 t1=new TestDaemonThread1();//creating thread
TestDaemonThread1 t2=new TestDaemonThread1();
TestDaemonThread1 t3=new TestDaemonThread1();
t1.setDaemon(true);//now t1 is daemon thread
t1.start();//starting threads
t2.start();
t3.start();
}
}
d) Aim: Write a JAVA program Producer Consumer Problem
Program:
class Queue
{
int x;
boolean is_data_present = false;
synchronized void store(int i)
{
try
{
if(is_data_present ==false)
{
x = i;
System.out.println(“Produced”+x);
is_data_present = true;
notify();
}
else
{
wait();
}
}
catch(Exception e)
{
System.out.println(“Some Problem Occured”);
}
}
synchronized void retrieve()
{
try
{
if(is_data_present ==true)
{
System.out.println(“Consumed”+x);
is_data_present = false;
notify();
}
else
{
wait();
}
}
catch(Exception e)
{
System.out.println(“Some Problem Occured”);
}
}
}
class Producer extends Thread
{
Queue a;
Producer(Queue q)

{
a = q;
}
public void run()
{
int j = 1;
for(;;)
{
a.store(j++);
}
}
}

class Consumer extends Thread


{
Queue b;
Consumer(Queue q)
{
b = q;
}
public void run()
{
for(;;)
{
b.retrieve();
}
}
}

class Threads
{
public static void main(String args[])
{
Queue q = new Queue();
Producer p = new Producer(q);
Consumer c = new Consumer(q);
p.start();
c.start();
}
}

You might also like