Results 1 to 3 of 3
  1. #1
    Lew
    Lew is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default How to use threads to generate numbers

    I am new to Java threads, and would like to use them to solve the following problem. Any help or references to similar problems is
    appreciated.

    The GenerateNumbers class is used by main to list integers (0,1,2,...) by:

    Java Code:
    GenerateNumbers g = new GenerateNumbers();
    while( true )
       System.out.println( g.getNumber() ) ;
    I know that you can easily do this without threads, but I want to use concurrency (as a learning exercise, and as my method easily generalizes to pairs, triples, etc.). All thread details must be "hidden" in the GenerateNumbers class, ie main doesn't care if GenerateNumbers does or does not use threads.

    Here's the outline to be completed.

    Java Code:
    public class GenerateNumbers
    {
       private int n;
    
       public GenerateNumbers ()
       {
          n = 0;
          // ?? set up thread and start generate ??
       }
    
       public int getNumber () 
       {
          // ?? resume generate ??
          return n--;
       }
    
       private void generate ()
       {
          while ( true ) 
          {
             // ?? wait (continue when resumed) ??
             n++;
          }
       }
    }
    Last edited by Lew; 02-08-2012 at 05:01 PM. Reason: simplify code

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: How to use threads to generate numbers

    Can you explain how you plan to use threads? What will each thread do? How will it interact with the other threads?
    How will execution pass from one thread to another?

  3. #3
    javin.paul is offline Member
    Join Date
    Oct 2011
    Posts
    35
    Rep Power
    0

    Default Re: How to use threads to generate numbers

    Adding on Norm's question, Yes without clear understanding of Threads and purpose it could be dangerous to use them. Threads are used to add parallelism in code e.g. animation where multiple things happening same time. In your case if you want to print numbers you can implemente runnable and put code of printing number inside run() method and when you create and start thread using java.lang.Thread that code will run in parallel with whatever there is left in main method or main thread.

Similar Threads

  1. Random Numbers using threads
    By subash22 in forum Threads and Synchronization
    Replies: 1
    Last Post: 06-05-2011, 04:09 PM
  2. Generate weighted random numbers, nextGaussian ()
    By graympa in forum New To Java
    Replies: 2
    Last Post: 03-25-2011, 10:36 PM
  3. Generate prime numbers within fiboncacci series?
    By Manish87 in forum New To Java
    Replies: 9
    Last Post: 08-08-2010, 05:07 AM
  4. generate pseudo random numbers in java
    By csr81 in forum Advanced Java
    Replies: 3
    Last Post: 03-01-2010, 08:08 AM
  5. Generate numbers around a circle?
    By pheonix in forum New To Java
    Replies: 4
    Last Post: 06-05-2009, 06:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •