In the precarious world of modern dairy farming, Jimmy Stup gambled and won.

Two years ago, when the milk price that farmers received was spiraling lower and some were selling cows or quitting altogether, Stup invested $3 million and doubled his herd, making his family farm one of the largest dairy operations in Frederick County.

Now, with grocery-store milk prices hovering near $4 a gallon, Stup is sitting pretty.

Still, when he bought a New Holland harvester recently, he picked a used model. Likewise for his Ford F-250 pickup. He could afford buying new ones, but it would have broken a rule of thumb around Teabow Farms: Never buy new what you can buy used, because farmers know that good times seldom last long.

"Just like a squirrel getting ready for winter, you're trying to hoard away some money -- because you know it's going to go back down," Stup, 43, said.

As milk prices have soared in recent months, farmers have been collecting their biggest paychecks in years. In May 2003, for example, farmers were bringing in about $11.00 per hundredweight -- the standard measure of raw milk. This year, the price rose to $20.30, according to the U.S. Department of Agriculture's Agricultural Statistics Board.

"It's never been that high. Never, never, never," Stanley Fultz, the University of Maryland's dairy science extension agent for Frederick, said last week. "And it won't be that high for long."

Few farmers are breaking out the champagne. For them, the increase has been welcome relief after a five-year spell of depressed dairy prices that found some farmers being paid less than the cost of production. Between summer 2002 and summer 2003, for example, the price of milk hit a 23-year low, causing dairy farmers to cut costs, trim their herds and tighten their belts. Many quit.

Even against a backdrop of steady declines in the number of all U.S. dairy farms, the decrease in dairy farms in the Washington area has been dramatic. In just five years, the number of Maryland dairy farms has fallen by about 26 percent, from 1,116 to 825, and Virginia has lost about 16 percent, from 1,887 to 1,580, according to the U.S. Department of Agriculture's 2002 Census, released last month.

"People just had to sell out," said Glenn Eaves Sr., whose family runs one of the state's largest dairy farms, north of Woodsboro. "Now that the price is up, there's just as many sales as before. A lot of the herds are going to Indiana and out west."

And in the past week, milk prices have dipped, while farmers contend with rising costs of feed, fertilizer and energy.

The recent price increase has caused stores to hang apologetic signs in the dairy aisle, forcing vendors to raise prices for a scoop of ice cream, and causing indigestion among consumers. Ben & Jerry's, for example, has passed along a 5 percent increase on all ice cream sold in stores and its 300 scoop shops, its first increase in 21/2 years and the largest in the company's history, spokeswoman Chrystie Heimert said.

The U.S. Department of Agriculture, in its monthly survey of retail prices, reported that a gallon of whole milk was $3.66 on average in June, up 32 percent from $2.77 last year.

"It's gone up a lot," said Jay Ellison, owner of the White Mountain Creamery, an ice cream shop in Rockville. Ellison said that he's paying $2.50 more for 21/2-gallon high-butterfat mixes of milk, cream and sugar that he uses as the base for his ice cream, and that he eventually must pass the cost on to consumers. "But I haven't raised my prices in seven years," he said.

At Shoppers Food Warehouse in White Flint, where milk was $3.89 a gallon two weeks ago, a neon-green sign was posted near the dairy case saying: "Due to the severe increase in the cost of milk, we have been forced to raise our prices. When our costs come back down, we will react accordingly." A similar sign hung over the dairy case in a Giant grocery store in Alexandria.

"Whoa! That's ridiculous," said Pam Paroline, 42, of Annandale, taking a break from work to pick up milk at the Giant. She said the rise in gasoline prices already has her weighing whether to turn on her vehicle's air conditioner during her daily commute, because the air conditioning cuts down on fuel efficiency. Now this.

"I'll be buying a smaller portion," Paroline said.

To Eaves, the memory of hard times is still fresh. To keep his Oak Bluff Dairy Farms running, Eaves had to borrow money from the bank.

Chuck Fry, who operates Rocky Point Farm in Frederick County, said his family relied on side businesses, such as raising turkeys and plowing snow, to make ends meet during the downturn.

"We didn't go out to dinner quite as much. We didn't buy a new car. We didn't buy a new tractor. We didn't take a big extravagant family vacation," he said. He was fortunate that his wife, Paula, could cover the family's health insurance because she had been a federal government employee.

Now that things have turned around, his family is planning a vacation to Disney World this week, said Fry, who is also president of the Frederick County Farm Bureau. But he's still wary.

"It's a nice year for the farmers," Fultz said. "But are they getting rich? No. The farmers have been suffering tremendously over the last three to five years, especially dairy farmers. They're digging out of a very deep financial hole."

Despite advances in biotechnology and labor-saving devices, the routine on dairy farms remains the same as it has for years, with workdays stretching from well before dawn until well after dusk, and chores as timeless as the herding of animals. And the food chain that connects the dairy farmer to the consumer is less direct than ever.

Many farms -- such as Eaves's and Fry's -- sell raw milk to cooperatives owned by farmers. Supply and demand hinge on everything from biological factors, such as how long it takes to raise a calf to milking age, to how much traders at the Chicago Mercantile Exchange bid for cheese.

What's more, in a practice dating to the Great Depression, the U.S. Department of Agriculture monitors the complexities of the dairy market and sets a base price that processors must pay farmers for fluid milk. The increase this year has caused producers and farmers to criticize the agency for boosting the price so severely at once -- at least 50 cents a gallon -- in May.

Those factors help make for an industry subject to punishing cycles of boom and bust.

"The bottom is going to fall out of this like it never happened," said James Vona, owner of Dairy Maid Dairy, a Frederick-based milk processor that supplies stores and schools in Northern Virginia, Maryland and District. "It's my feeling we're going to see wild swings in the market."

The United States is losing about half of its dairy farms each decade, said Larry Salathe, senior USDA economist. The latest price rise owes to a combination of factors, he said.

Low dairy prices in recent years thinned herds and pushed some farmers out of business. The National Milk Producers Federation, a cooperative of dairy farmers, initiated a buyout program. Herds were further reduced after the United States closed the Canadian border to livestock imports after an outbreak of mad cow disease in May 2003.

Then came shortages of a genetically engineered hormone that enhances a cow's milk production. Monsanto Co. began rationing recombinant bovine somatotropin, or rbST, after federal inspectors found quality-control problems in its production plant.

Even the Atkins diet has been cited for driving up dairy prices, because the all-protein regimen has boosted demand for cheese and has caused some farmers to sell milk cows for slaughter.

But farmers have said that when the price of milk goes up in grocery stores, it stays up, even after their receipts have come down.

"Retail prices are increasingly disconnected from the farm-level price," said Amber DuMont, a spokeswoman for the Virginia and Maryland Milk Producers Cooperative Association, based in Reston.

At Teabow Farms in Walkersville, Stup said he recalls when he and his family considered selling their farm and buying another out west during the lean times. Instead, they plunged ahead with the $3 million expansion.

"Last year in June and July, it was pretty hard to swallow what we'd done. There were nights we said to ourselves, 'What did we do?' " Stup said. "But we knew it's a roller coaster."

Kim Dreyer helps milk cows at Rocky Point Farm in Frederick County. For some farms, milk price increases have been a blessing.

Pam Paroline, 42, shops for milk in Alexandria as Andre Evans stocks cartons near a sign that explains the price increases. Frederick County dairy farmer Chuck Fry pulls a reluctant heifer into a stall, helped by Kim Dreyer and Brandon Trout.