0% found this document useful (0 votes)
13 views12 pages

Java Threading Examples and Concepts

The document contains multiple Java programs demonstrating thread creation and management, including extending the Thread class and implementing Runnable. It covers concepts such as thread synchronization with isAlive and join methods, daemon threads, and the producer-consumer problem. Each section includes code examples and explanations of the functionality being illustrated.

Uploaded by

lavanya.d
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)
13 views12 pages

Java Threading Examples and Concepts

The document contains multiple Java programs demonstrating thread creation and management, including extending the Thread class and implementing Runnable. It covers concepts such as thread synchronization with isAlive and join methods, daemon threads, and the producer-consumer problem. Each section includes code examples and explanations of the functionality being illustrated.

Uploaded by

lavanya.d
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

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

You might also like