Java program to print prime numbers using Thread

By: Paawan Chaudhary  

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();
	}
}	



Archived Comments

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

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

3. Great looking site. Presume you did a lot of your own coding.


[url=http://for

View Tutorial          By: boutique bio at 2017-06-17 19:09:12

4. Thanks pertaining to offering these types of great posting.



[url=http

View Tutorial          By: hypnose lyon at 2017-06-17 13:29:35

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial