my last male did that occasionally. Since he was always in sight, I was always able to get him out on the deck. With him there wasn't the humping so much as just standing with his back arched, everything "on display", and an embarrassed look on his face.
There was no reversing the process once it started so I just put him on the deck or in the grass.
He did it periodically until a few months before he died. Thank God it never happened when we had company!