Wow I'm glad I'm not the only one with this issue, and oddly I find it's worse as I get older. It's called peruresis. When I was in school I could sometimes do the urinal, like if I was going in with my friends, though it was hit and miss, like normally I'd use the stall to play it safe especially in other public washrooms, but high school I was the most comfortable. Stall was no issue. Now, even stall is an issue. I'll be standing there, and it just refuses to come out. The more noise going on in the bathroom or the less isolated I am the worse it is. If there's only like 1 stall and I know someone is waiting for me, forget it. Though there is an odd twist to that, if it's a very large bathroom with like 10+ stalls, busy or not, I have less trouble. Worse is small busy bathrooms with 1-2 stalls max.
I noticed that it got worse when I went to a local hockey game a few years back and went to pee in the stall and it just refused to come out. I BADLY had to go too. Luckly there is a restaurant and I went to order a poutine and while waiting I slipped into that restaurant's bathroom to try again without anyone noticing and I succeeded as there was nobody there.
Since I discovered that it's gotten worse I dread travel now. Something like a local event is not so bad because I figure worse case scenario I'm having to hold it in for a few hours till I get home, and I'll make sure to not drink that day in preparation for said event.
The whole thing is just a mind game, but very hard to control. If I think about it before even needing to go to the bathroom, I know I'm done for because it will just stay on my mind. I just hope that it's something that goes away, because before I had zero issues with doing it in the stall but now even that is hard depending on the bathroom/situation.