tl;dr: Use pomodoros as a real-life 'busy status' indicator.
Most people do not interrupt out of malice, or even lack of consideration. They do it to be sociable, because they like you and want to interact. As such, stopping them from doing so is probably going to pass a bad message.
On the other hand, given they do it because they like you, if they are made aware from visible cues that their interruption will cause you grief at a particular point you're trying to focus, then they will be more likely to wait until you look less busy next time.
So to an extent, this is also your failure in that you accommodate such interruptions without signifying that they were inappropriately timed.
As a possible solution, I recommend applying the pomodorro technique, both in terms of ensuring your focus, but also as a strong visible social cue that an interruption would be inappropriate. If you're not familiar with it, basically you set a VISIBLE (or even somewhat audible!) timer for 25 minute slots (with 5-minute breaks), during which you're expected to have ABSOLUTE focus on your task. If an interruption occurs, you postpone its action for when the pomodoro period is over, unless it's impossible to avoid, in which case the pomodoro is reset.
Therefore, say Jim comes and says hello, while you've got a pomodorro running. You go, "Ah, Jim! One sec." You save everything and close the coding window you were working on, take your headphones off, visibly reset the pomodorro, and rise to greet. If he asks what's that you say exactly what it is. You don't have to be passive aggressive, but explain that it is there to ensure uninterrupted focus, but, oh well it's ok, but, actually if you don't mind, if you see me doing a pomodorro next time please don't interrupt because it means I'm trying to focus ... but if I'm not doing one please do come and say hi".
The pomodorro then becomes a real-life "Status: Busy" icon for your coworkers, and it not ticking becomes a real-life "Available for Chat" icon :p
Of course, there will always be the odd toxic co-worker who pretends not to get it and interrupts anyway, but presumably your question was more general. But in that case, you should be assertive and straightforward about it: "please don't interrupt when I'm doing a pomoddoro, it's severely disrupting my thoughts and takes me forever to get back to that point".
Also, if you're going to use a pomodoro, make sure it reflects reality. If you've got one running but you're clearly playing solitaire, pretty soon it will cease to be a valid marker to your coworkers and they'll ignore it. Not to mention, sticking to pomodorros can be a great productivity and time management tool, so you should stick to it anyway.