Java program to print prime numbers using Thread

By: Paawan Chaudhary Viewed: 155688 times  Printer Friendly Format    


This is a Java program to implement two threads such that one thread prints prime numbers from 1 to 10 and other thread prints non-prime numbers from 1 to 10 (use Thread class ).
Note :- Each thread has a delay of 500 millsecond after printing one number

import java.lang.*;
import java.io.*;
import java.util.*;
class Prime extends Thread
{
	public void run()
	{
		try
		{
			for(int i=1;i<=10;i++)
			{
				if(i==2||i==3||i==5||i==7)
				{
					System.out.println ("Prime No.= "+i);
				}
				Thread.sleep(500);
			}
		}
		catch (Exception e){}
	}
}
class notPrime extends Thread
{
	public void run()
	{
		try
		{
			for(int i=1;i<=10;i++)
			{
				if(i==4||i==6||i==8||i==9||i==10)
				{
					System.out.println ("Non-Prime No.= "+i);
				}
				Thread.sleep(500);
			}
		}
		catch (Exception e){}
	}
}
class q13Thread
{
	public static void main(String args[])
	{
		new Prime().start();
		new notPrime().start();
	}
}	


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. dfggdfh
View Tutorial          By: h at 2016-07-26 10:46:10

2. aobbazafo
View Tutorial          By: aobbazafo at 2017-02-27 17:53:40

3. waiyekuyorag
View Tutorial          By: waiyekuyorag at 2017-02-27 18:03:15

4. ivafodxmop
View Tutorial          By: ivafodxmop at 2017-02-27 18:35:22

5. uwixoyokvita
View Tutorial          By: uwixoyokvita at 2017-02-27 18:44:38

6. zgiduyize
View Tutorial          By: zgiduyize at 2017-02-27 18:57:17

7. xvexyavonipop
View Tutorial          By: xvexyavonipop at 2017-02-27 19:49:37

8. amodwulo
View Tutorial          By: amodwulo at 2017-02-27 19:50:43

9. avivuqukeyl
View Tutorial          By: avivuqukeyl at 2017-02-27 19:53:19

10. ixipuicucimbo
View Tutorial          By: ixipuicucimbo at 2017-02-27 19:57:45

11. bomomeqoime
View Tutorial          By: bomomeqoime at 2017-02-27 20:16:34

12. yogdwanuwibi
View Tutorial          By: yogdwanuwibi at 2017-02-28 03:15:47

13. inapxauuxugar
View Tutorial          By: inapxauuxugar at 2017-02-28 23:00:37

14. uhomziko
View Tutorial          By: uhomziko at 2017-03-01 16:43:58

15. iteqholuduc
View Tutorial          By: iteqholuduc at 2017-03-01 17:06:59

16. oqabetaha
View Tutorial          By: oqabetaha at 2017-03-02 11:05:31

17. agekeyey
View Tutorial          By: agekeyey at 2017-03-02 11:27:48

18. esktazo
View Tutorial          By: esktazo at 2017-03-03 09:16:43

19. Thanks pertaining to offering these types of great
View Tutorial          By: hypnose lyon at 2017-06-17 13:29:35

20. Great looking site. Presume you did a lot of your
View Tutorial          By: boutique bio at 2017-06-17 19:09:12

21. hiksiss
View Tutorial          By: hiksiss at 2017-07-02 13:22:22

22. nodxocCep
View Tutorial          By: nubqvsCep at 2017-08-08 06:43:58