Why does the truck rev up to 2000 to 3000 rpms in park and other times idles low on 2002 Chevrolet Silverado 2500

The truck started with it not idleing so i changed the fuel filter and it went away. Now it will idle up to 3000 rpms in park or neutral and about 1500 in gear even on the brake or not. I disconnected the battery and it went away for awhile. It pretty much runs high all the time now. Ive had other times it will idle down to 500 rpms and stall out too. Some times it will run fine when you first hop in but will act up in a short time. Any ideas?

Asked by for the 2002 Chevrolet Silverado 2500