10 Replies - 347 Views - Last Post: 08 October 2012 - 01:20 PM Rate Topic: -----

#1 Rover2cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 12-February 11

Multithread program not picking up where last thread left off.

Posted 07 October 2012 - 05:11 PM

Hello my thread is not picking up where the last thread started off.
I have four threads running
t1.start
t2.start ... etc
Why is it doing this
2 is Prime.One
2 is Prime.Three
2 is Prime.Four
2 is Prime.Two
3 is Prime.Four
3 is Prime.Three
3 is Prime.One
5 is Prime.Three
5 is Prime.Four
3 is Prime.Two
7 is Prime.Four
7 is Prime.Three

package javaapplication9;



public class JavaApplication9 implements Runnable{	
		
String name;
public JavaApplication9(String s) {
    name = s;
    
}

           int count = 1;
           int num = 3;
           int[] primes = new int[10];
          
           public void run(){
              primes[0] = 2;
            
                
		

             
		// Find the first 10 primes
		while (count<10) {
			if (isPrime(num)) {
				primes[count] = num; 
				count++;
			}
			num = num + 1;
		}
		
		
        
		// Prepares the output
		for (int i=0; i<primes.length; i++)	{
		 System.out.println (primes[i] + " is Prime." + name );
                }}
		


    public static boolean isPrime(int num) {


    if (num%2==0) return false;

    for(int i=3;i*i<=num;i+=2) {
        if(num%i==0)
            return false;
    }
    return true;
}}


This post has been edited by Rover2cool: 07 October 2012 - 05:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Multithread program not picking up where last thread left off.

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: Multithread program not picking up where last thread left off.

Posted 07 October 2012 - 05:42 PM

The first and most important thing to know about threads: Their execution order is not guaranteed. (1) Threads are dealt with on the OS-level, so there is no guarantee of behavior across platforms, and (2) even if scheduling algorithms were consistent across platforms, the scheduler isn't likely to impose any specific order on the threads chosen to run. You might as well say it's random (even though it's not).

Quote

Hello my thread is not picking up where the last thread started off.


Now are the threads not completing? Is the program hanging? Or have you self-imposed some kind of order of execution that you're not seeing happen? Two very different things. E.g. If this is the output thus far in program:

2 is Prime.One
2 is Prime.Three



You can't expect that 2 is Prime.Four will be next. It may be this time, but there will be 1 run out of the next 10, 100, or 1000 runs where it won't be.

This post has been edited by blackcompe: 07 October 2012 - 05:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 Rover2cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 12-February 11

Re: Multithread program not picking up where last thread left off.

Posted 07 October 2012 - 06:11 PM

View Postblackcompe, on 07 October 2012 - 05:42 PM, said:

The first and most important thing to know about threads: Their execution order is not guaranteed. (1) Threads are dealt with on the OS-level, so there is no guarantee of behavior across platforms, and (2) even if scheduling algorithms were consistent across platforms, the scheduler isn't likely to impose any specific order on the threads chosen to run. You might as well say it's random (even though it's not).

Quote

Hello my thread is not picking up where the last thread started off.


Now are the threads not completing? Is the program hanging? Or have you self-imposed some kind of order of execution that you're not seeing happen? Two very different things. E.g. If this is the output thus far in program:

2 is Prime.One
2 is Prime.Three



You can't expect that 2 is Prime.Four will be next. It may be this time, but there will be 1 run out of the next 10, 100, or 1000 runs where it won't be.


Yes I understand threads are random. I was trying to get it to execute like this:
2 is Prime thread Three
3 is Prime thread Four
5 is Prime thread One
7 is Prime thread Two
11 is Prime thread One
13 is Prime thread Four
17 is Prime thread Three

This post has been edited by Rover2cool: 07 October 2012 - 06:13 PM

Was This Post Helpful? 0
  • +
  • -

#4 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Multithread program not picking up where last thread left off.

Posted 07 October 2012 - 06:20 PM

To start with, you are printing the full array in each of your threads, and so you are guaranteed for every thread to list all of the first 10 prime numbers.

With your current code, I think you also run the risk of a prime being listed multiple times in the array in the scenario where:

  • Threads A and B read values of num and count at the same time
  • Thread A inserts into array and increases count
  • Thread B inserts into array and increases count again


Another potential problem (which you might be concerned about) is that the order the numbers in the array is not guaranteed.
Was This Post Helpful? 0
  • +
  • -

#5 Rover2cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 12-February 11

Re: Multithread program not picking up where last thread left off.

Posted 07 October 2012 - 06:30 PM

View Postrfs02, on 07 October 2012 - 06:20 PM, said:

To start with, you are printing the full array in each of your threads, and so you are guaranteed for every thread to list all of the first 10 prime numbers.

With your current code, I think you also run the risk of a prime being listed multiple times in the array in the scenario where:

  • Threads A and B read values of num and count at the same time
  • Thread A inserts into array and increases count
  • Thread B inserts into array and increases count again


Another potential problem (which you might be concerned about) is that the order the numbers in the array is not guaranteed.


sigh... I dont know..First time doing threads. Im in a OS class that makes us do threads in java for a project but we was never taught threads in java 1..Java 1 was the requirements to get into this class too..
Was This Post Helpful? 0
  • +
  • -

#6 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Multithread program not picking up where last thread left off.

Posted 07 October 2012 - 06:42 PM

You really need to get comfortable with locking variables before you update them.

Effectively, every thread needs to 1. lock num, 2. read the value, 3. unlock the value and then check if it's prime.

Before you update the array, lock count, increment it, unlock it and then insert into array.

Then print the array from your main thread, not one of the worker threads.
Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: Multithread program not picking up where last thread left off.

Posted 07 October 2012 - 06:43 PM

Rover2cool: Can you give a high-level description of what you're trying to do? Perhaps a link to the assignment instructions. Because saying that you need the output to be such and such isn't saying much. It appears (from what little code you've provided) that you're sending this runnable instance to multiple threads, where as rfs02 said, it's likely that you're going to encounter concurrency issues. And still, as I said before, it's not reasonable to try and impose an execution order (at least not without some synchronization). For an OS class, synchronization is a key concept, so I wouldn't be surprised if your assignment required it.

Quote

You really need to get comfortable with locking variables before you update them.


Exactly.

Quote

Yes I understand threads are random. I was trying to get it to execute like this:
2 is Prime thread Three
3 is Prime thread Four
5 is Prime thread One
7 is Prime thread Two
11 is Prime thread One
13 is Prime thread Four
17 is Prime thread Three


Please explain what order this is? Should only the primes come out in order? Should only the threads come out in order? Should they come out sequentially or can they skip around? Ascending order? Or do you need that exact output? Giving a good description of the task will help to get away from this kind of confusion.

This post has been edited by blackcompe: 07 October 2012 - 06:48 PM

Was This Post Helpful? 0
  • +
  • -

#8 Rover2cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 12-February 11

Re: Multithread program not picking up where last thread left off.

Posted 07 October 2012 - 09:11 PM

View Postblackcompe, on 07 October 2012 - 06:43 PM, said:

Rover2cool: Can you give a high-level description of what you're trying to do? Perhaps a link to the assignment instructions. Because saying that you need the output to be such and such isn't saying much. It appears (from what little code you've provided) that you're sending this runnable instance to multiple threads, where as rfs02 said, it's likely that you're going to encounter concurrency issues. And still, as I said before, it's not reasonable to try and impose an execution order (at least not without some synchronization). For an OS class, synchronization is a key concept, so I wouldn't be surprised if your assignment required it.

Quote

You really need to get comfortable with locking variables before you update them.


Exactly.

Quote

Yes I understand threads are random. I was trying to get it to execute like this:
2 is Prime thread Three
3 is Prime thread Four
5 is Prime thread One
7 is Prime thread Two
11 is Prime thread One
13 is Prime thread Four
17 is Prime thread Three


Please explain what order this is? Should only the primes come out in order? Should only the threads come out in order? Should they come out sequentially or can they skip around? Ascending order? Or do you need that exact output? Giving a good description of the task will help to get away from this kind of confusion.


PROBLEM 2 MULTITHREADED PRIME-NUMBER GENERATOR
Write a program that produces a list of prime numbers between 1 and N. The program accepts two input parameters including N, the maximum number to test for primality and T, the number of threads that compute the results. For example, the following execution would use 8 threads to compute prime numbers from 1 to 10000: java PrimeGenerator 10000 8 The list of prime numbers should be written, one per line, to standard output (System.out.println). The time required to produce the list should be written to the standard error stream (System.err.println). The format of the timing output should be a comma-separated record containing the values N, T, and the time required to complete the program in milliseconds. The following example would be a possible result of the above execution. (The timing values are fabricated and just an example of format). 10000,8,2000


For the implementation, use a class variable X in the main class to keep track of the highest number to test for primality. X should be accessible by all threads during execution of the program. As each thread becomes ready for work, it should increment the value of X and determine if the new value is prime or not. Values that are prime numbers should be printed to standard output. Because multiple threads may read the value of X simultaneously, expect some duplication of answers.
Was This Post Helpful? 0
  • +
  • -

#9 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Multithread program not picking up where last thread left off.

Posted 07 October 2012 - 09:26 PM

OK. So this simplifies things for you quite considerably.

  • You don't have to worry about synchronization
  • You are not looking for the first n primes, you are looking for primes between 1 and N, so no need for an array
  • You don't need to store the results - just print them to screen


Other than that, the last paragraph in your assignment definition more or less tells you what you need to do.
Was This Post Helpful? 1
  • +
  • -

#10 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: Multithread program not picking up where last thread left off.

Posted 07 October 2012 - 09:41 PM

rfs02 summed up perfectly what you need to do.
Was This Post Helpful? 0
  • +
  • -

#11 Rover2cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 12-February 11

Re: Multithread program not picking up where last thread left off.

Posted 08 October 2012 - 01:20 PM

Delete.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1