Java

As part of my Operating Systems class, we were asked to create a Simple Slot Machine project.  Some important topics that were addressed are: Concurrency, Threads and Synchronization. 

What is concurrency?
How many programs are you running on your OS (Operating System) right now? ...I guarantee you there is more than one program running!  While you are playing music off the network (Pandora?), typing an English (or any other class) paper, and have your browser running (even though you may not be using it or have minimized it).  All these programs are always ready to respond to mouse events or keyboard, no matter how busy they are.  This is called concurrency.
An example of concurrency in SlotMachine is having all my three reels (reel1, reel2, reel3) "spinning" at the same time.
Threads
A thread, just like the word spells it out, it is a thread of execution in a program (yes, many threads can execute at the same time).  Three threads were created in three separate classes (ReelOne, ReelTwo, ReelThree).  I could have put them all in one class, but that was just the way I did it...  Each thread executes its part (regardless of what the other two are doing).  The execution of my thread is done by randomly change the icons (ImageIcons) on the labels (JLabels).
Synchronization
Since Threads share access to fields and the objects reference fields, synchronization is a tool needed to prevent thread interferences errors and memory consistency errors.

Here is an example of one of the synchronized run() methods for ReelOne that extends the Thread class:
public synchronized void run(){
                Random rGen = new Random();
                while(SlotMachine.stop[0]==false){
                    try{
                 Thread.sleep(100);  
                 SlotMachine.label[0].setIcon(SlotMachine.icons[rGen.nextInt(SlotMachine.icons.length)]);
                    } 
                    catch (InterruptedException x){System.err.println ("Error");}
                } 
                SlotMachine.stop[0] = false;   //Set to false in order to be able to start() threads again!
 }  

Classes (BlueJ IDE)

Copyright © Paola Garcia

GUI (execution of main() method)

Copyright © Paola Garcia

Ok, so... there is a light modification to my program.  This modification allows for NO CODE REPETITION... Since we do not know if in the future we would like to have more reels (maybe you find that in Vegas), it would be annoying having to write a separate class for each of the reels right? (What if we have a 100 reels???).  They all behave the same (spin at the same speed) when a thread is created... So there is no need on creating separate classes.

Instead, just one class for the reels was created (SlotReel).  This class already assigns the sleep() time for all the reels -100-, and passes a parameter (int labelNum) that is going to be useful in the SlotMachine class.

Here is a picture of my ONLY 2 classes... (Is already looking much better and less complicated, isn't it?)

Copyright © Paola Garcia

If you would like to take a look at part (or ALL) of the program, or if you have any comments/suggestions, please let me know!

March 2011