Peter Wagner was attending a business dinner recently when the man sitting next to him began to drone on about annuities and asset allocation. Ten minutes in, he’d had enough.

Smiling, he said: “We’ve just about exhausted my interest in this topic. What else have you got?”

His companion paused briefly, then grinned and switched the topic to Caitlin Clark and women’s basketball.

Bet you wish you had Houdini-like escape skills like that!

There’s an art to extracting yourself from a boring conversation . Done right, you escape, and the other person still feels good about the connection.

Too often, though, we feel stuck when someone is yammering on about something we find tedious. Rather than ending the chat or changing the subject, we stay silent, nodding politely and panicking inside. We fear being rude or hurting the other person’s feelings. Or we simply don’t know how to move on.

“A conversation is a little bit like driving down the freeway,” says Adam Mastroianni , an experimental psychologist who studies conversations . “There are only certain points where you are allowed to enter or exit without doing considerable damage.”

Research by Mastroianni and others shows that in just 2% of conversations do two people want to stop talking at the same point and manage to do so. The vast majority of the time, people in a conversation differ on when they want to end it, with one person wishing to stop talking an average of 10 minutes—or 68% of the length of the conversation—sooner than the other.

We need to take a lesson from rats. (Yes, rats!) When put in a maze, they typically just hang out. But give them a trigger—heat up the floor or administer a shock to their feet—and they quickly discover how to get the heck out. Scientists call this “escape conditioning.”

Consider boredom your trigger to make a break for it. Research shows that people would rather self-administer electric shocks than be bored. One reason: It takes a lot of mental effort to try to stay engaged when we’re bored.

“It’s like a dieter holding back from chocolate cake or a smoker from having a smoke,” says Philip Gable , a professor of psychological and brain sciences at the University of Delaware, who studies the neural effects of boredom.

Excuses that work

I heard a number of effective ways to put an end to a dull conversation while reporting this column, including this time-tested standby: “I’m going to get a drink. Can I get you one?”

Many people said that they pretend their spouse or significant other is calling. (One couple has a code-word to alert each other that they need to be saved.) Others said they try to pass the long-winded person off on someone else, then flee.

A woman in her 80s says she waits for the person who is rattling on to take a breath and then starts talking about whatever she wants. “If someone is going to be boring, I’d rather it was me,” she says.

Be kind

Think of listening as a gift.

A few years ago, Greg Reid was waiting for a flight when the man sitting next to him struck up a conversation. Reid was catching up on work email and wanted to continue. But he decided to listen as the man talked about his kids, his job and his workout routine.

When it came time to board the flight, Reid says that the man shook his hand and said: “Thank you, that was wonderful. I haven’t enjoyed a conversation like this since my wife recently passed away.”

“I am glad I listened,” says Reid, 59, a data privacy consultant in Portland, Maine. “You never know what the other person is going through.”

Make a game of it

The best way to escape a boring conversation is to turn it into a better one, says Mastroianni.

Challenge yourself to make the chat interesting. Give yourself bonus points for each thing you discover that you and the other person have in common.

Todd Kelman’s strategy is to pepper the person with questions until he learns something they can laugh about. He did this with a woman he met at a recent networking event and learned that she was a vegan. “Are you a legit type of vegan or someone who has-a-couple-glasses-of wine-and-orders-a-double-cheeseburger type of vegan?” he teased her.

The woman admitted she’d been a vegan for just two months. He then confessed that he’d lied about how much he worked out. And soon the two were laughing and bantering. “Boring conversation averted,” says Kelman, 49, the manager of a professional hockey team in Cardiff, Wales.

Try to pull someone else in

Introducing someone new to the person you’re talking to can break up a monologue and broaden the conversation, Gable says.

My nephew and I have another strategy. Before big family events, we remind each other about something interesting we did recently that we want to talk about. Then, if we get stuck in a conversation we don’t like, we throw the spotlight on each other. “Hey, did you know that Noah got straight A’s this semester?” I said recently to a family friend at a party who was talking a little too long about how he keeps his lawn free of weeds.

It’s OK to flee if all else fails

Elisabeth Crain , a psychotherapist in Santa Barbara, Calif., suggests you give a three-minute warning, just like you would to a toddler. Try: “I’m afraid I have to be leaving in the next few minutes, but I want to hear the end of your story before I go.”

This makes the person feel validated and avoids abruptness. Thank the person for their time and tell them you hope to see them again.

“If you make people feel seen and heard, they’ll have a good memory of you,” says Crain.

Wagner says he’s been using his “you’ve pretty much exhausted my interest” line for years. Rather than ending the conversation, he’ll often try to redirect it, suggesting a new topic such as: “How ‘bout those Yankees?”

So far, he says no one’s appeared to be offended.

“If you say it in a friendly way with a smile on your face, it works like a charm,” says Wagner, 67, an executive in Roslyn, N.Y.

Write to Elizabeth Bernstein at Elizabeth.Bernstein@wsj.com