java - Number Generator using BlockingQueue -
i'm new in java programming. i', working on homework have use concurrency in our code. i've read a lot of resources don't know how solve problem. has 5 parts if me understand first part i'm sure can rest myself. please me do? here first part description:
there 1 thread job hand out numbers testing. thread run code in class called numbergenerator. thread should use instance of class implements java blockingqueue interface method of storing available numbers , sharing other threads need it. have 1 instance of class running in thread, , cannot run in main thread.
here code:
/** * numbergenerator class used hand out numbers checked * other threads. */ public class numbergenerator { /** biggest number plan check */ private final int biggestnumbertocheck; /** * queue place numbers generate, need * checked */ private final blockingqueue<integer> candidatequeue; private final int smallesrnumberstart; // todo: other state need? /** * create new instance of numbergenerator class, hand out * numbers need checked. * * @param smallest * smallest number check * @param biggest * biggest number check * @param queue * queue put numbers check */ public numbergenerator(int smallest, int biggest, blockingqueue<integer> queue) { biggestnumbertocheck = biggest; candidatequeue = queue; smallesrnumbertostart = smallest; } // todo: can decide how want turn runnable // thread. }
to create thread have 2 options:
- extend thread
- implement runnable
so, pick one. in either case, must override/implement run
method holds code run in created thread.
iiuc, in case method generate random numbers , put them in queue.
then, in main
method, there instance of numbergenerator
class instantiated appropriate implementation of blockingqueue
, pick one, there several of them.
finally, call start
method of thread
instance begin generation of numbers.
Comments
Post a Comment