Week-7
a) Write a JAVA program that creates
threads by extending the Thread class. The
first thread displays “Good Morning” every 1
second, the second thread displays “Hello”
every 2 seconds, and the third displays
“Welcome” every 3 seconds (Repeat the
same by implementing Runnable).
Program:
class GoodMorning extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
sleep(1000); [Link]("good morning");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class Hello extends Thread
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
sleep(2000); [Link]("hello");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class Welcome extends Thread
{
public void run()
Week-7
{
try
{
for(int k=1;k<=10;k++)
{
sleep(3000); [Link]("welcome");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class ThreadDemo1
{
public static void main(String args[])
{
GoodMorning a1=new GoodMorning();
Hello b1=new Hello();
Welcome c1=new Welcome();
[Link]();
[Link]();
[Link]();
}
}
Week-7
Output:
Week-7
(Repeat the same by implementing Runnable).
Program:
class GoodMorning implements Runnable
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
[Link](1000); [Link]("good morning");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class Hello implements Runnable
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
[Link](2000); [Link]("hello");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class Welcome implements Runnable
{
Week-7
public void run()
{
try
{
for(int k=1;k<=10;k++)
{
[Link](3000); [Link]("welcome");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class ThreadDemo
{
public static void main(String args[])
{
GoodMorning a1=new GoodMorning();
Hello b1=new Hello();
Welcome c1=new Welcome();
Thread t1=new Thread(a1);
Thread t2=new Thread(b1);
Thread t3=new Thread(c1); [Link]();
[Link]();
[Link]();
}
}
Week-7
Output:
Week-7
b) Write a program illustrating is Alive and join ().
Aim: Creating a thread class by extending Thread
class and use isAlive and join methods
Program:
public class MyThread extends Thread
{
public void run()
{
[Link]("r1 ");
try
{
[Link](500);
}
catch(InterruptedException ie)
{
}
[Link]("r2 ");
}
public static void main(String[] args)
{
MyThread t1=new MyThread();
MyThread t2=new MyThread();
[Link]();
try
{
[Link](); // Waiting for c1 to finish
}
catch (InterruptedException ie)
{
}
[Link]();
[Link]([Link]());
[Link]([Link]());
}
}
Output:
Week-7
c) Write a program illustrating Daemon Threads.
Aim: Illustrating Daemon
Threads.
Description:
A daemon thread is a thread that does not prevent the JVM from exiting when
the program finishes but the thread is still running. An example for a daemon
thread is the garbage collection. You can use the setDaemon(boolean) method
to change theThread daemon properties before the thread starts.
Program:
public class DaemonThreadExample1 extends Thread
{
public void run()
{
// Checking whether the thread is Daemon or not
if([Link]().isDaemon())
{
[Link]("Daemon thread executing");
}
else
{
[Link]("user(normal) thread executing");
}
}
public static void main(String[] args)
{
/* Creating two threads: by default they are
* user threads (non-daemon threads)
*/
Week-7
DaemonThreadExample1 t1=new DaemonThreadExample1();
DaemonThreadExample1 t2=new DaemonThreadExample1();
//Making user thread t1 to Daemon
[Link](true);
//starting both the threads
[Link]();
[Link]();
}
}
Output:
d) Write a JAVA program for the Producer Consumer Problem.
Aim: Java program on producer
consumer problem. Description:
The producer–consumer problem (also known as the bounded-buffer problem)
is a classic example of a multi-process synchronization problem. The problem
describes two processes, the producer and the consumer, who share a common,
fixed-size buffer used as a queue.
Program
class A
{
int n;
boolean b=false;
synchronized int get()
{
if(!b)
try
{
Week-7
wait();
}
catch(Exception e)
{
[Link](e);
}
[Link]("Got:"+n);
b=false;
notify();
return n;
}
synchronized void put(int n)
{
if(b)
try
{
wait();
}
catch(Exception e)
{
[Link](e);
}
this.n=n;
b=true;
[Link]("Put:"+n);
notify();
}
}
class producer implements Runnable
{
A a1;
Thread t1;
producer(A a1)
{
this.a1=a1;
t1=new Thread(this);
[Link]();
}
public void run()
{
for(int i=1;i<=10;i++)
{
[Link](i);
}
}
}
class consumer implements Runnable
{
A a1;
Thread t1;
Week-7
consumer(A a1)
{
this.a1=a1;
t1=new Thread(this);
[Link]();
}
public void run()
{
for(int j=1;j<=10;j++)
{
[Link]();
}
}
}
class ProducerConsumer
{
public static void main(String args[])
{
A a1=new A();
producer p1=new producer(a1);
consumer c1=new consumer(a1);
}
}
Output:
Week-7