Write a program to define two threads one will print 1 to 10 numbers whereas other will print 11 to 20 numbers

In this program, we have to create two threads, one will print numbers from 1 to 10 and other will print numbers from 11 to 20. The OneTo10 thread will print 1 to 10 numbers and ElevenTo20 thread will print 11 to 20 numbers. The start( ) method starts the execution of thread.


PROGRAM
class OneTo10 extends Thread {
 
 public void run() {
  
  for(int i=1; i<=10; i++) {
   System.out.println("OneTo10 : " + i);
  }
 }
}

class ElevenTo20 extends Thread {
 
 public void run() {
  
  for(int i=11; i<=20; i++) {
   System.out.println("ElevenTo20 : " + i);
  }
 }
}

public class OneToTenThread {

 public static void main(String[] args) {
  
  new OneTo10().start();
  new ElevenTo20().start();
 }
}
OUTPUT
C:\>javac OneToTenThread.java
C:\>java OneToTenThread
OneTo10 : 1
OneTo10 : 2
OneTo10 : 3
OneTo10 : 4
OneTo10 : 5
OneTo10 : 6
OneTo10 : 7
OneTo10 : 8
OneTo10 : 9
OneTo10 : 10
ElevenTo20 : 11
ElevenTo20 : 12
ElevenTo20 : 13
ElevenTo20 : 14
ElevenTo20 : 15
ElevenTo20 : 16
ElevenTo20 : 17
ElevenTo20 : 18
ElevenTo20 : 19
ElevenTo20 : 20

Comments

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Program to input age from user and throw user-defined exception if entered age is negative

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.