Threads and Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • YASIN786
    New Member
    • Feb 2007
    • 12

    Threads and Arrays

    hi ol

    my name is yasin and am currently working on a java task for a friend.

    the scenario is:
    there are two threads thr1 and thr2 and one array arr1 with a capacity of 100.
    the array is populated with random numbers in the range of 1-100.
    the objective of the task is to find all the numbers in the array arr1 which are in the range of 70-80 using the thread thr1 and to send these numbers to the second thread thr2.

    Any ideas/advice will highly be appreciated.

    thanks.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by YASIN786
    hi ol

    my name is yasin and am currently working on a java task for a friend.

    the scenario is:
    there are two threads thr1 and thr2 and one array arr1 with a capacity of 100.
    the array is populated with random numbers in the range of 1-100.
    the objective of the task is to find all the numbers in the array arr1 which are in the range of 70-80 using the thread thr1 and to send these numbers to the second thread thr2.

    Any ideas/advice will highly be appreciated.

    thanks.
    So the array 'belongs' to thr1; this thread sends particular numbers to thr2. This makes
    thr1 a 'producer' and 'thr2' a consumer where the number is the 'shared resource'.
    There should be a 'semaphore' indicating that a number can be consumed or a new
    number should be produced. Both threads wait on that condition; the consumer
    delivers a new number and sets the condition; otherwise it waits.

    The producer waits on the opposite of the condition (the previous number wasn't
    consumed yet) and delivers a next number and resets the condition. Both threads
    notify each other when they've done what they must do.

    kind regards,

    Jos

    Comment

    Working...