Extended phase space thermodynamics for black holes in a cavity