courses:cs334-201503:assignments:javathreadshomework.docx (14.3 KB)

advertisement
Cmpu334 Operating Systems
HW: Time to write some threaded code. Date due: Thursday, October, 8, 2015. Yes, it is the same date
as the midterm.
Similar to the thread programs we examined in the lab, create 2 worker threads that will each update
two ArrayLists with 5000 values. Perhaps, Demo5 is the closest model, although you can use the
JTExtends sets of project files too. Create a third thread to print out and verify (!) the size of the
ArrayLists to be sure that each is of length 10000. Also, let’s perform some data collection and print out
the time it takes for all of the worker threads to do their work.

Create a ThreadedArrayList() class/project that has
o Two ArrayList members
o Two “add to list” methods, say addToListA() and addTtoListB()
o A main() routine that creates the 3 threads and prints out the threaded execution time
once the threads are finished.
 The first two threads will each have a run method that will call the two add to
list methods
o The third thread will have a run method that verifies each list to be of size 10,000 and
issues a success or fail message. The run method can simply call verify() and will perform
the testing for you. The third thread has to … wait for it… really. It literally cannot be
started until the first two threads are finished. Is that too much of a hint?
Important notes:
1. For data collection you can get the start time and end time of a task using:
public static long currentTimeMillis()
Returns the current time in milliseconds. Note that while the unit of time of the return value is a
millisecond, the granularity of the value depends on the underlying operating system and may be
larger. For example, many operating systems measure time in units of tens of milliseconds.
2. You may use the Random class (don’t forget java.util.Random!) to create random numbers to
add to the list.
3. Run the project several times to be sure that the results are consistent!
4. (What keyword is needed to be sure of consistent results?)
5. The submit script has been setup in the Asprey lab. If you prefer, you may email the project to
me instead.
Download