You can also use the Compressor object in WPILib. The way it works is to start a background thread that polls the pressure switch periodically and turns the compressor on and off by itself. So you don't need loops and don't need to look at the switch or operate the Spike relay that controls the compressor.
To use it see this page for a C++ example, but the Java version is almost the same:
http://wpilib.screenstepslive.com/s/...for-pneumatics
Brad