Computer Science, asked by khan17089, 1 year ago

i will mark as brainliest

Write a program to create two threads, so one thread will print even numbers

 between 1 to 10 whereas other will print odd numbers between 11 to 20.​

Answers

Answered by siddhartharao77
7

(i) Thread(String):

It is used for creating an object of thread class by specifying the name of thread.

Syntax: Thread t1 = new thread("th");

(ii) Thread(Runnable):

It is used for converting runnable object interface object into thread class object for making use of all the methods of thread class.

Sample Program to create two threads:

class Brainly

{

public static void main(String args[])

{

Runnable run2 = new Runnable1();

Thread t1 = new Thread(run2);

Runnable run1 = new Runnable2();

Thread t2 = new Thread(run1);

t2.start();

t1.start();

}

}

class Runnable2 implements Runnable

{

public void run()

{

int i = 0;

System.out.println("Even numbers are: ");

while(i < 11)

{

System.out.println(i);

i = i + 2;

}

}

}

class Runnable1 implements Runnable

{

public void run()

{

int j = 11;

System.out.println("Odd numbers are: ");

while(j <= 20)

{

System.out.println(j);

j = j + 2;

}

}

}

Output:

Even numbers are:

0

2

4

6

8

10

Odd numbers are:

11

13

15

17

19

Hope it helps!

Attachments:
Similar questions