0% found this document useful (0 votes)
18 views8 pages

Exercise 7

easily learning for students

Uploaded by

Keerthi bolimera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Exercise 7

easily learning for students

Uploaded by

Keerthi bolimera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

7)a)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)

Using Thread class


class First extends Thread
{
Thread t;
First()
{
t=new Thread(this);
System.out.println("Good Morning");
t.start();
}
public void run()
{
for(int i=1;i<5;i++)
{
System.out.println("Good Morning"+i);
try{
t.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class Second extends Thread
{
Thread t;
Second()
{
t=new Thread(this);
System.out.println("hello");
t.start();
}
public void run()
{
for(int i=1;i<5;i++)
{
System.out.println("hello"+i);
try{
t.sleep(2000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class Third extends Thread
{
Thread t;
Third()
{
t=new Thread(this);
System.out.println("welcome");
t.start();
}
public void run()
{
for(int i=1;i<5;i++)
{
System.out.println("welcome"+i);
try{
t.sleep(3000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
public class MultiThreadWithThtead
{
public static void main(String arg[])
{
new First();
new Second();
new Third();
}
}
Output
Using Runnable Interface
class First implements Runnable
{
Thread t;
First()
{
t=new Thread(this);
System.out.println("Good Morning");
t.start();
}
public void run()
{
for(int i=1;i<5;i++)
{
System.out.println("Good Morning"+i);
try{
t.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class Second implements Runnable
{
Thread t;
Second()
{
t=new Thread(this);
System.out.println("hello");
t.start();
}
public void run()
{
for(int i=1;i<5;i++)
{
System.out.println("hello"+i);
try{
t.sleep(2000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class Third implements Runnable
{
Thread t;
Third()
{
t=new Thread(this);
System.out.println("welcome");
t.start();
}
public void run()
{
for(int i=1;i<5;i++)
{
System.out.println("welcome"+i);
try{
t.sleep(3000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
public class MultiThreadWithRunnable
{
public static void main(String arg[])
{
new First();
new Second();
new Third();
}
}
OUTPUT:

7)b)Write a program illustrating isAlive() and join ()


Java Source Code

//using isAlive() and join() methods


class OwnThread implements Runnable
{
String name;
Thread t;
OwnThread(String threadName) {
name=threadName;
t=new Thread(this,name);
t.start();
}
public void run() {
try
{
for (int i=1;i<5 ;i++ )
{
System.out.println(name+"......>"+i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("......Our Thread interrupted......");
}
System.out.println(name+"...... exiting......");
}
}
class JoinEx {
public static void main(String[] args)
{
OwnThread obj1=new OwnThread("One");
OwnThread obj2=new OwnThread("Two");
OwnThread obj3=new OwnThread("Three");
System.out.println("...... Thread one is alive......"+obj1.t.isAlive());
System.out.println("...... Thread two is alive......"+obj2.t.isAlive());
System.out.println("...... Thread three is alive......"+obj3.t.isAlive());
try
{
System.out.println("........waiting for threads to finish............");
obj1.t.join();
obj2.t.join();
obj3.t.join();
}
catch (InterruptedException e)
{
System.out.println("......Main Thread interrupted.....");
}
System.out.println("...... Thread one is alive......"+obj1.t.isAlive());
System.out.println("...... Thread two is alive......"+obj2.t.isAlive());
System.out.println("...... Thread three is alive......"+obj3.t.isAlive());
}
}
Output
7)c)Write a Program illustrating Daemon Threads.
Java Source Code

public class DaemonThread extends Thread{


public void run(){
// Checking whether the thread is Daemon or not
if(Thread.currentThread().isDaemon()){
System.out.println("Daemon thread executing");
}
else{
System.out.println("user(normal) thread executing");
}
}
public static void main(String[] args){

try
{
for (int i=1;i<5;i++ )
{
System.out.println(".............Main Thread ....."+i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("........... Main interrupted.........");
}
/* Creating two threads: by default they are
user threads (non-daemon threads)
*/
DaemonThread t1=new DaemonThread();
DaemonThread t2=new DaemonThread();
//Making user thread t1 to Daemon
t1.setDaemon(true);
//starting both the threads
t1.start();
t2.start();
}
}
Output

7)d)Write a JAVA program Producer Consumer Problem


Java Source Code
//using wait() and notify() methods
class Producer extends Thread {
StringBuffer sb;
Producer() {
sb=new StringBuffer();
}
public void run()
{
synchronized(sb)
{
for(int i=0;i<10;i++)
{
try
{
sb.append(i+":");
Thread.sleep(100);
System.out.println(i+".......produced and
appending....");

}
catch (InterruptedException e)
{
System.out.println(".......producer interrupted....");
}
}
sb.notify();
}
}
}
class Consumer extends Thread {
Producer prod;
Consumer(Producer prod) {
this.prod=prod;
}
public void run()
{
synchronized(prod.sb)
{
try
{
prod.sb.wait();
}
catch (InterruptedException e)
{
System.out.println(".......consumer interrupted....");
}
System.out.println(prod.sb);
}
}
}
class InterThreadCommunication
{
public static void main(String[] args)
{
Producer obj1=new Producer();
Consumer obj2=new Consumer(obj1);
Thread t1=new Thread(obj1);
Thread t2=new Thread(obj2);
t2.start();
t1.start();
}
}
Output

You might also like