0% found this document useful (0 votes)
22 views

Multi Threading

The document demonstrates multithreading in Java by creating three thread classes - evenThread, oddThread, and multiple5Thread - that each print even, odd, or multiples of 5 from 1 to 20/50 while sleeping between outputs. The main method instantiates objects of each thread class and uses join() to wait for their completion before terminating the main thread.

Uploaded by

Omar Farooq
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Multi Threading

The document demonstrates multithreading in Java by creating three thread classes - evenThread, oddThread, and multiple5Thread - that each print even, odd, or multiples of 5 from 1 to 20/50 while sleeping between outputs. The main method instantiates objects of each thread class and uses join() to wait for their completion before terminating the main thread.

Uploaded by

Omar Farooq
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

MULTITHREADING

PROGRAM:

import java.io.*;
class evenThread extends Thread
{
evenThread()
{
super("demothread");
start();
}

public void run()


{
try
{
for(int i=1;i<=20;i=i+2)
{
System.out.println("even no"+i);
Thread.sleep(200);
}
}

catch(InterruptedException ie)
{
System.out.println(ie);
}
System.out.println("even thread terminates");
}
}

class oddThread extends Thread


{
oddThread()
{
super("demothread");
start();
}

public void run()


{
try
{
for(int i=1;i<=20;i=i+1)
{
System.out.println("odd no"+i);
Thread.sleep(200);
}
}

catch(InterruptedException ie)
{
System.out.println("even thread terminates");
}
}
}

class multiple5Thread extends Thread


{
multiple5Thread()
{
super("demothread");
start();
}

public void run()


{
try
{
for(int i=1;i<=50;i=i+5)
{
System.out.println("even no"+i);
Thread.sleep(500);
}
}

catch(InterruptedException ie)
{
System.out.println("multilpe5 thread terminates");
}
}
}

class sample
{
public static void main(Stirng str[])
{
evenThread t=new evenThread();
oddThread t1=new oddThread();
multiple5Thread t2=new multilpe5Thread();
try
{
Thread.join();
Thread1.join();
Thread2.join();
}

catch(InterruptedException ie)
{
System.out.println(ie);
}
System.out.println("Main thread terminates");
}
}

You might also like