3 Replies - 11154 Views - Last Post: 15 February 2009 - 11:55 AM Rate Topic: -----

#1 vodkanyone  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 27-November 08

Using Two Threads For Prime Numbers

Post icon  Posted 15 February 2009 - 08:13 AM

Need two threads- thread one computes number of primes from 1 - 2*n/3 and thread two should compute from 2*n/3 - n. Then will add both thread counts and print it out.

When n is 20 the output is


Thread 1 contains 5 primes
Thread 1 contains 0 primes
Number of primes is: 5
Thread 2 contains 0 primes
Thread 2 contains 3 primes
Number of primes is: 3

This is obviously wrong as output should be

Thread 1 contains 5 primes
Thread 2 contains 3 primes
Number of primes is: 8



package JavaThread;

/**
* Summary description for Program
*/
class Primes extends Thread
{
	int threadie;
	public Primes(int threadID)
	{
		threadie = threadID;
	}

	public void run()
	{
		int n = 20;
		int c = 2 * n / 3;
		int a = 0;
		int b = 0;
		

		for (int i = 0; i < n; i++)
		{
			if(threadie == 1){

				if (i < c)
				{
					if ((Primes.isPrime(i))) { a++;  }
				}
			}

			if(threadie == 2){
				if (i >= c )
			{
				if ((Primes.isPrime(i))) { b++; }
			}}
		}

	
		System.out.println("Thread " + threadie + " contains " + a + " prime numbers ");
		System.out.println("Thread " + threadie + " contains " + b + " prime numbers ");
		int NumPrimes = a + b;
		System.out.println(" Number of primes is: " + NumPrimes);
}
	


	static boolean isPrime(long n)
	{
		if (n <= 1) return false;
		double limit = Math.sqrt(n);
		for (long i = 2; i <= limit; i++)
		{
			if (n % i == 0) return false;
		}
		return true;
	}


	public static void main(String[] arg)
	{

		Thread th1 = new Primes(1);
		Thread th2 = new Primes(2);
		th1.start();
		th2.start();
		try { th1.join(); }
		catch (InterruptedException ie) { }
		try { th2.join(); }
		catch (InterruptedException ie) { }
		
		System.out.println("\nThreads 1 and 2 have finished");
	}
}


		





Is This A Good Question/Topic? 0
  • +

Replies To: Using Two Threads For Prime Numbers

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Using Two Threads For Prime Numbers

Posted 15 February 2009 - 09:31 AM

a, b and NumPrimes are local variables inside the threads so they don't know about each other.
As a quick fix to get it working you could make a and b static and calculate NumPrimes in main()
/**
* Summary description for Program
*/
class Primes extends Thread
{
	int threadie;
	static	 int a = 0;
	static	 int b = 0;

	public Primes(int threadID)
	{
		threadie = threadID;
	}

	public void run()
	{
		int n = 20;
		int c = 2 * n / 3;
	//	int a = 0;
   //	 int b = 0;
	   

		for (int i = 0; i < n; i++)
		{
			if(threadie == 1){

				if (i < c)
				{
					if ((Primes.isPrime(i))) { a++;  }
				}
		   }

			if(threadie == 2){
				if (i >= c )
			{
				if ((Primes.isPrime(i))) { b++; }
			}}
		}

   
		   if(threadie == 1)  System.out.println("Thread " + threadie + " contains " + a + " prime numbers ");
		   if(threadie == 2)  System.out.println("Thread " + threadie + " contains " + b + " prime numbers ");
  //	 int NumPrimes = a + b;
  //	  System.out.println(" Number of primes is: " + NumPrimes);
}
   


	static boolean isPrime(long n)
	{
		if (n <= 1) return false;
		double limit = Math.sqrt(n);
		for (long i = 2; i <= limit; i++)
		{
			if (n % i == 0) return false;
		}
		return true;
	}


	public static void main(String[] arg)
	{

		Thread th1 = new Primes(1);
		Thread th2 = new Primes(2);
		th1.start();
		th2.start();
		try { th1.join(); }
		catch (InterruptedException ie) { }
		try { th2.join(); }
		catch (InterruptedException ie) { }
	   
		System.out.println("\nThreads 1 and 2 have finished");
	   int NumPrimes = a + b;
		System.out.println(" Number of primes is: " + NumPrimes);
	}
}


I am sure there is a more elegant way of doing this though

a run give
Thread 1 contains 5 prime numbers 
Thread 2 contains 3 prime numbers 

Threads 1 and 2 have finished
 Number of primes is: 8


This post has been edited by horace: 15 February 2009 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Using Two Threads For Prime Numbers

Posted 15 February 2009 - 11:28 AM

and what was wrong with your first topic? :(
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Using Two Threads For Prime Numbers

Posted 15 February 2009 - 11:55 AM

this will slightly improve the performance of your isPrime() method
static boolean isPrime(long n)
	{
		if(n == 2)return true;
		if (n < 2 || n % 2 == 0) return false;
		for (long i = 3; i*i <= n; i += 2)
		{
			if (n % i == 0) return false;
		}
		return true;
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1