You are on page 1of 5

1) WAP to demostrate making of thread to print numbers from 1 to 10.

public class firstten implements Runnable {

public void run()


{
int i;
for (i=0;i<=10;i++)
{
System.out.println(i);
}
}
}

class Main
{
public static void main(String[] args) {
firstten f1 = new firstten();
Thread t1 = new Thread(f1);
t1.start();
}
}

2) WAP that has two threads. One of the threads displays odd numbers and other displays
even numbers.

import java.util.Scanner;

/**
* Created by akshay on 11/10/2016.
*/
class Odd extends Thread
{
public void run()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
for (int i=1; i<=n; i++)
{
if(i%2!=0)
{ System.out.println(i); }
}
}
}

class Even extends Thread


{
public void run()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
for (int i=1; i<=n; i++)
{
if(i%2==0)
{ System.out.println(i); }
}
}

PDF created with pdfFactory Pro trial version www.pdffactory.com


}

class demoMain
{
public static void main(String args[])
{
Odd t1=new Odd();
Even t2= new Even();
t1.start();
t2.start();
}
}

4) WAP that has two threads. One of the threads displays fibonacci series and the other
displays prime numbers upto n.
import java.util.Scanner;

/**
* Created by akshay on 11/10/2016.
*/
public class fibo extends Thread {

int a=0,b=1,c;
public void run()

{
Scanner sc = new Scanner(System.in);
System.out.println("Enter x for fibonacci");
int x = sc.nextInt();
System.out.println(a);
System.out.println(b);
for(int i=1;i<=x-2;i++)
{
c=a+b;
System.out.println(c);
a=b;
b=c;
}
}
}

class Prime extends Thread {

public void run() {


int n, p;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number n upto which prime nos are needed");
n = sc.nextInt();
for (int i = 2; i < n; i++) {
p = 0;
for (int j = 2; j < i; j++) {
if (i % j == 0)
p = 1;
}
if (p == 0)
System.out.println(i);
}
}
}

class Maine

{
public static void main(String[]args){

PDF created with pdfFactory Pro trial version www.pdffactory.com


fibo f1 = new fibo();
Prime f2 = new Prime();
f1.start();
System.out.println( );
f2.start();

}
}

5) WAP to display alphabetss and numbers with the help of multithreading.


/**
* Created by akshay on 11/10/2016.
*/
class abcd extends Thread{

char ch='a';
public void run() {
for (int i = 0; i <= 26; i++)
{
System.out.println(ch++);
}

}
}

class numb extends Thread{

public void run()


{
for(int i=0;i<10;i++)
{
System.out.println(i);
}
}
}

class Mainee
{
public static void main(String[] args) {
abcd a1 = new abcd();
numb n1 = new numb();
a1.start();
n1.start();
}
}

6) WAP to print */*/*/*/*/ using two child threads.


/**
* Created by akshay on 11/10/2016.
*/class ajeeb extends Thread{

char ch = 'a';

public void run() {


System.out.print("*/*/*");

PDF created with pdfFactory Pro trial version www.pdffactory.com


}

class nimbu extends Thread


{

public void run()


{
System.out.println("/*/*/");
}
}

class Maineee
{
public static void main(String[] args) {
ajeeb a1 = new ajeeb();
nimbu n1 = new nimbu();
a1.start();
n1.start();
}
}

7) WAP to show the use of sleep method.


import com.sun.xml.internal.bind.v2.runtime.output.SAXOutput;

/**
* Created by akshay on 11/10/2016.
*/
public class sleep {

public static void main(String[] args) throws InterruptedException {


System.out.println("The output will be printed after 4s");

int i;
for(i=0;i<=10;i++)
{
System.out.println(i);
Thread.sleep(4000);
}

8) WAP to demonstrate isAlive() and join() method.


/**
* Created by akshay on 11/10/2016.
*/
class alivejoin extends Thread {

public void run()


{
System.out.println("hell");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("heaven");
}

public static void main(String[] args) throws InterruptedException {

PDF created with pdfFactory Pro trial version www.pdffactory.com


alivejoin a1 = new alivejoin();
alivejoin a2 = new alivejoin();
a1.start();
System.out.println(a1.isAlive());
System.out.println(a2.isAlive());
a1.join();
a2.start();
System.out.println(a1.isAlive());
System.out.println(a2.isAlive());

PDF created with pdfFactory Pro trial version www.pdffactory.com

You might also like