Cramps happen to every athlete, providing us all with the occasional vicious, burning, gunshot-wound-feeling disruption to an otherwise blissful workout. Cramps are the result of a muscle repeating the same motion one too many times, and the mechanism for telling a contracted muscle to relax simply craps out. The cause, in short, is muscle fatigue.
This puts endurance athletes in particular at risk. Athletes can reduce their risk by guarding against fatigue with plenty of water, and electrolyte drinks for outings longer than one and a half hours, and by eating lots of carbohydrates every twenty or thirty minutes. Tradition suggests that bananas stave off cramps, which is debatable, but it can't hurt.
But beyond such givens as eating and drinking, stretching seems to be the only real remedy. Work on the problem muscles, which are generally quadriceps and hamstrings for cyclists. If cross training, work on quads and calves for running and toe flexors and calves for swimming.
If you do get a cramp in mid-exercise, apply ice for fifteen minutes. If without such conveniences, skip straight to the next step: gently massage the muscle with you knuckles in a back-and-forth motion for three minutes, which will nudge the relaxation reflex into doing its job.
Once the cramp is gone, continue the workout fairly vigorously, say at 70 percent of you aerobic maximum. Resist the temptation to go real easy, as moderate exercise is required to pump the lactic acid out of your muscles.