0% found this document useful (0 votes)
6 views5 pages

Java Thread Programming Examples

Uploaded by

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

Java Thread Programming Examples

Uploaded by

sampath oruganti
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2-BTECH-CSE:: I SEM

OOPS THROUGH JAVA PROGRAMMING LAB


EXPERIMENT: 7
7(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)
import [Link].*; class Three implements Runnable
class One extends Thread {
{ public void run()
public void run() {
{ for(int i=0;i<100;i++)
for(int i=0;i<100;i++) {
{ try{
try{ [Link](3000); }
[Link](1000); } catch(InterruptedException e){
catch(InterruptedException e){ [Link](e); }
[Link](e); } [Link]("Wel come");
[Link]("Good }
Morning"); }
} } } }
class ThreadEx
class Two extends Thread {
{ public static void main(String[] args)
public void run() {
{ One t1=new One();
for(int i=0;i<100;i++) Two t2=new Two();
{ Three tt=new Three();
try{ Thread t3=new Thread(tt);
[Link](2000); } [Link]("One");
catch(InterruptedException e) [Link]("Two");
{ [Link]("Three");
[Link](e); [Link](t1);
} [Link](t2);
[Link]("Hello "); [Link](t3);
} Thread t=[Link]();
} [Link](t);
} [Link]();[Link]();[Link]();
} }
OUTPUT:
c:\Lab>javac [Link]
c:\Lab>java ThreadEx
Thread[One,5,main]
Thread[Two,5,main]
Thread[Three,5,main]
Thread[main,5,main]
Good Morning
Good Morning
Hello
Wel come
Good Morning
Hello
Good Morning
Good Morning
Hello
Wel come
Good Morning
Good Morning
Hello
Good Morning
7(B): WRITE A JAVA PROGRAM USING THREAD CLASS METHODS ?
SOURCE CODE:

class A extends Thread {


public void run() {
for ( int i = 1; i <= 5; i++ ) {
[Link]("Thread A ... i = " + i);
if ( i == 3 ) {
try {
sleep(800); // sleep for 800 ms
} catch(Exception e) { }
} }
[Link]("Exit from Thread A : Execution over");
}}

class B extends Thread {


public void run() {
for ( int i = 11; i <= 15; i++ ) {
[Link]("Thread B ... i = " + i);
if ( i == 12 ) {
yield(); // relinquish control to some other thread
} }
[Link]("Exit from Thread B : Execution over");
}}

class C extends Thread {


public void run() {
for ( int i = 21; i <= 25; i++ ) {
[Link]("Thread C ... i = " + i);
if ( i == 23 ) {
stop(); // thread execution stops
} }
[Link]("Exit from Thread C : Execution over");
}
}

public class ThreadMethodDemo {


public static void main(String args[]) {
new A().start();
new B().start();
new C().start();
} }
7(c): Write a java Program illustrating Daemon Threads.

Source code -1:


public class DaemonDemo1 extends Thread
{
public DaemonDemo1(String name1)
{
super(name1);
}
public void run()
{
if([Link]().isDaemon())
{
[Link](getName() + " is Daemon thread");
}
else
{
[Link](getName() + " is User thread");
} }
public static void main(String[] args)
{
DaemonDemo1 D1 = new DaemonDemo1("D1");
DaemonDemo1 D2 = new DaemonDemo1("D2");
DaemonDemo1 D3 = new DaemonDemo1("D3");

[Link](true);
[Link]();
[Link]();
[Link](true);
[Link]();
}
}

Output:
Source code -2:

public class DaemonDemo1 extends Thread


{
public DaemonDemo2(String name1)
{
super(name1);
}
public void run()
{
if([Link]().isDaemon())
{
[Link](getName() + " is Daemon thread");

}
else
{
[Link](getName() + " is User thread");
}
[Link](getName()+" priority "+[Link]().getPriority());
}
public static void main(String[] args)
{
DaemonDemo2 D1 = new DaemonDemo2("D1");
DaemonDemo2 D2 = new DaemonDemo2("D2");
DaemonDemo2 D3 = new DaemonDemo2("D3");

[Link](true);
[Link]();
[Link]();
[Link](true);
[Link]();
}
}

Output:
D1 is Daemon thread
D1 priority 5
D2 is User thread
D3 is Daemon thread
D2 priority 5
D3 priority 5

You might also like