l O M oA R c P S D | 3 6 5 7 7 5 2 3
EXERCISE – 7
a)Extending Thread class
AIM: To 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 2seconds and the third display
“Welcome” every 3 seconds ,(Repeat the same by implementing Runnable)
(i) Creating multiple threads using Thread class
class A extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
sleep(1000);
[Link]("good morning");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class B extends Thread
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
sleep(2000);
[Link]("hello");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
l O M oA R c P S D | 3 6 5 7 7 5 2 3
class C extends Thread
{
public void run()
{
try
{
for(int k=1;k<=10;k++)
{
sleep(3000);
[Link]("welcome");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class threaddemo
{
public static void main(String args[])
{
A a1=new A();
B b1=new B();
C c1=new C();
[Link]();
[Link]();
[Link]();
}
}
OUT-PUT:
good morning
hello
good morning
good morning
welcome
hello
good morning
good morning
hello
good morning
welcome
good morning
hello
good morning
good morning
l O M oA R c P S D | 3 6 5 7 7 5 2 3
welcome
hello
good morning
hello
welcome
hello
welcome
hello
hello
welcome
hello
welcome
welcome
welcome
welcome
(ii) Creating multiple threads using Runnable interface
class A implements Runnable
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
[Link](1000);
[Link]("good morning");
}
}
catch(Exception e)
{
[Link](e);
}
}}
class B implements Runnable
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
[Link](2000);
[Link]("hello");
}
l O M oA R c P S D | 3 6 5 7 7 5 2 3
}
catch(Exception e)
{
[Link](e);
}
}
}
class C implements Runnable
{
public void run()
{
try
{
for(int k=1;k<=10;k++)
{
[Link](3000);
[Link]("welcome");
}
}
catch(Exception e)
{
[Link](e);
}
}}
class runnabledemo
{
public static void main(String args[])
{
A a1=new A();
B b1=new B();
C c1=new C();
Thread t1=new Thread(a1);
Thread t2=new Thread(b1);
Thread t3=new Thread(c1);
[Link]();
[Link]();
[Link]();
}
}
OUT-PUT:
good morning
good morning
hello
good morning
welcome
l O M oA R c P S D | 3 6 5 7 7 5 2 3
good morning
hello
good morning
good morning
welcome
hello
good morning
good morning
hello
good morning
welcome
good morning
hello
welcome
hello
hello
welcome
hello
welcome
hello
hello
welcome
welcome
welcome
welcome
(b)Implementing isAlive() and join()
AIM: To write a program illustrating isAlive and join ()
class A extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
sleep(1000);
[Link]("good morning");
}
}
catch(Exception e)
{
[Link](e);
}
}
l O M oA R c P S D | 3 6 5 7 7 5 2 3
}
class B extends Thread
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
sleep(2000);
[Link]("hello");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class C extends Thread
{
public void run()
{
try
{
for(int k=1;k<=10;k++)
{
sleep(3000);
[Link]("welcome");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class isalivedemo
{
public static void main(String args[])
{
A a1=new A();
B b1=new B();
C c1=new C();
[Link]();
[Link]();
l O M oA R c P S D | 3 6 5 7 7 5 2 3
[Link]();
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
try
{
[Link]();
[Link]();
[Link]();
}
catch(InterruptedException e)
{
[Link](e);
}
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}
OUT-PUT:
true good morning
true hello
true welcome
good morning hello
good morning hello
hello welcome
good morning hello
welcome welcome
good morning hello
hello hello
good morning welcome
good morning welcome
welcome welcome
hello welcome
good morning false
good morning false
hello false
good morning
welcome
l O M oA R c P S D | 3 6 5 7 7 5 2 3
c) Implementation of Daemon Threads
AIM: To write a program illustrating Daemon Threads
class A extends Thread
{
public void run()
{
if([Link]().isDaemon())
[Link]("daemon thread work");
else
[Link]("user thread work");
}
}
class daemondemo
{
public static void main(String[] args)
{
A a1=new A();
A a2=new A();
A a3=new A();
[Link](true);
[Link]();
[Link]();
[Link]();
}
}
OUT-PUT:
daemon thread work
user thread work
user thread work
d) Producer-Consumer problem
AIM: Write a JAVA program Producer Consumer Problem
class A
{
int n;
boolean b=false;
synchronized int get()
{
if(!b) try
{
wait();
}
l O M oA R c P S D | 3 6 5 7 7 5 2 3
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;
l O M oA R c P S D | 3 6 5 7 7 5 2 3
consumer(A a1)
{
this.a1=a1;
t1=new Thread(this);
[Link]();
}
public void run()
{
for(int j=1;j<=10;j++)
{
[Link]();
}}}
class interdemo
{
public static void main(String args[])
{
A a1=new A();
producer p1=new producer(a1);
consumer c1=new consumer(a1);
}
}
OUT-PUT:
Put:1
Got:1
Put:2
Got:2
Put:3
Got:3
Put:4
Got:4
Put:5
Got:5
Put:6
Got:6
Put:7
Got:7
Put:8
Got:8
Put:9
Got:9
Put:10
Got:10