Write a Java program which will create two child threads by implementing Runnable interface. One thread will print even numbers from 1 to 50 and other display vowels.
Write a Java program which will create two child threads by implementing Runnable interface. One thread will print even numbers from 1 to 50 and other display vowels.
Program:
import java.io.*; import java.util.*; public class EvenNumberVowel implements Runnable { String name; Thread t; EvenNumberVowel(String str) { name = str; t = new Thread(this, name); t.start(); } public void run() { name = Thread.currentThread().getName(); if(name.equals("First")) { for(int i=1;i<=50;i++){ try { if(i%2==0) { System.out.println("First:"+i); t.sleep(500); } } catch(InterruptedException ie) { System.out.println(ie); } } } else if(name.equals("Second")) { char vowels[] = {'A','E','I','O','U','a','e','i','o','u'}; int len = vowels.length; for(int i=0;i<len;i++) { try { System.out.println("Second:"+vowels[i]); t.sleep(500); } catch(InterruptedException ie) { System.out.println(ie); } } } } public static void main(String args[]) { EvenNumberVowel t1 = new EvenNumberVowel("First"); EvenNumberVowel t2 = new EvenNumberVowel("Second"); } }
Comments
Post a Comment