CIS 636 Spring 2006                                         Assignment 6 – Simple Threads in Java                                                        100 points

Assigned: 03/14/2006

Due: 03/28/2006 at the start of class  -

 

You may work individually or in pairs for this assignment. But all work must be the work of the person/people whose name is on the code! If working in pairs, the individual contributions should be relatively equal. One possibility is to work together tonight, then finish separately (to avoid communication difficulties).

Main Assignment:

We are going to use related but unsynchronized threads to work in (sort of) parallel on a problem. The task involves checking Powerball entries to see if they are (big) winners. The task will be divided up among threads so that each thread works on a particular entry (in real world we might do this for a range of entries, as with the primes example). Each thread will check the entry that it is responsible for (the entry will store the result as an instance variable, by using the provided Entry class and storing the entry in the class that you create, you will not be working with shared data – so you don’t have to worry about synchronization). You have been provided a Entry class  (see my WWW page) that you can make use of.  Your task is to

·         write a class that provides thread capabilities for this task. The class should include an instance variable to hold an Entry that the thread is to work on, an instance variable to keep track of the winning numbers, and an instance variable to hold the Powerball number. (these are necessary because start() and run() do not take any parameters).  You may set up your threads either extending Thread or implementing Runnable; I did it using Thread. Either way, you will need a constructor that allows getting the info mentioned above into the class.

·         write a main that 1) kludges together an array of Entrys and a winning set of numbers (5 balls numbered 1-55, and 1 Powerball numbered 1-42) to allow testing your other work; 2) displays the entries  (Entry’s toString method can be used); 3) starts a thread for each entry, 4) waits for all threads to end using join(), 5) retrieves the results from each thread, and 6) produces final summary results (how many winners, most number of matches). 

Hand in:

To avoid needless long waits, I recommend working on this on the c: drive, then upon completion, copying it to the a: drive, testing it there, creating an extra backup for yourself, then deleting it from the c: drive.

Miscellaneous:

·         The Entry class provides a lot of capability. Look into what it provides; no need to reinvent the wheel.

·         Winning numbers could be created by creating a random Entry (that’s what I did – so in the sample interaction when you see the winning numbers being displayed, they are just an Entry “Sold At” “winner”).

·         In order to avoid the need for synchronization, we do not use any shared structures. The flip side of this is that when the threads are finished, we need to do further investigation of each entry outside of the thread. This obviously loses some of the advantages of threads, but live with it (don’t try to do this using synchronization – we might not have covered it that well yet)

·         MAKE SURE YOUR PROGRAM WORKS! (i.e. more than just removing compile errors).   With the long odds on winning, you may not get any winners, but be prepared to show them.

·         Put YOUR NAME, and e-mail address, date in comments at the beginning of the program.

·         Also, comment any significant code to document it (each method, each loop, each if, and each significant calculation should have a comment. In particular, each method needs to have a JAVADOC compatible comment).

·         Indent code following standard conventions (indent to show that something is “inside” or “part of” the preceding code (e.g. inside if’s or loops, or statements continued on a new line).  The IDE should handle this if you hit ENTER and move on to the next line (If you make changes, you may need to TAB to make things right).

·         Name your variables meaningfully, to describe their use in the program.

·         There is a fair amount to this assignment.  A good strategy for working on a challenging program is to build and test it in parts (e.g. get the setup working, then add next capability and test that, etc)

Sample Interaction:


 

Entry created: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  11 19 23 27 29 PB: 40 Not Checked Yet

Entry created: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  6 23 24 30 33 PB: 0 Not Checked Yet

Entry created: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  12 14 30 34 40 PB: 24 Not Checked Yet

Entry created: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  12 39 46 47 50 PB: 39 Not Checked Yet

Entry created: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  16 21 33 49 53 PB: 33 Not Checked Yet

Entry created: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  3 8 21 28 50 PB: 2 Not Checked Yet

Entry created: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  20 21 27 42 52 PB: 7 Not Checked Yet

Entry created: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  4 16 33 34 52 PB: 4 Not Checked Yet

Entry created: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  6 7 8 27 38 PB: 13 Not Checked Yet

Entry created: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  1 5 34 48 53 PB: 16 Not Checked Yet

 

Winning Numbers: Drawing Date: 3/14/2006 Sold At: winner Numbers:  8 29 31 35 36 PB: 9 Not Checked Yet

 

Thread: Thread-0 checked: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  11 19 23 27 29 PB: 40 Matches: 1 PB: no match no win

Thread: Thread-1 checked: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  6 23 24 30 33 PB: 0 Matches: 0 PB: no match no win

Thread: Thread-2 checked: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  12 14 30 34 40 PB: 24 Matches: 0 PB: no match no win

Thread: Thread-3 checked: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  12 39 46 47 50 PB: 39 Matches: 0 PB: no match no win

Thread: Thread-4 checked: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  16 21 33 49 53 PB: 33 Matches: 0 PB: no match no win

Thread: Thread-5 checked: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  3 8 21 28 50 PB: 2 Matches: 1 PB: no match no win

Thread: Thread-6 checked: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  20 21 27 42 52 PB: 7 Matches: 0 PB: no match no win

Thread: Thread-7 checked: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  4 16 33 34 52 PB: 4 Matches: 0 PB: no match no win

Thread: Thread-8 checked: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  6 7 8 27 38 PB: 13 Matches: 1 PB: no match no win

Thread: Thread-9 checked: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  1 5 34 48 53 PB: 16 Matches: 0 PB: no match no win

Largest number of matches: 1

     By: Drawing Date: 3/14/2006 Sold At: Wayne Wawa Numbers:  11 19 23 27 29 PB: 40 Matches: 1 PB: no match no win

Number of winners: 0