NEW YORK — The gunman who fatally ambushed two police officers in their squad car had a long criminal record, a hatred for police and the government, and an apparent history of mental instability that included an attempt to hang himself a year ago, authorities said. Moments before opening fire, Ismaaiyl Brinsley approached people on the street in Brooklyn and asked them to follow him on Instagram, then told them, “Watch what I'm going to do,” Chief of Detectives Robert Boyce said. A portrait of the Brooklyn-born gunman emerged as big-city police departments and union leaders around the U.S. warned officers to change their routines and insist on extra backup a day after Brinsley carried out what he portrayed online as retaliation for the slayings of black men at the hands of white police officers. Brinsley was black; the slain officers were ethnically Asian and Hispanic. The slayings come at a tense time. Police across have been criticized for months for their tactics, following Eric Garner's death in a New York officer's chokehold and Michael Brown's fatal shooting in Ferguson, Missouri. Protests erupted in recent weeks after grand juries declined to charge the white officers involved. Investigators were trying to determine if Brinsley had taken part in any protests over the deaths of Brown and Garner, whose names he invoked in his online threat, or simply latched on to the cause for the final act in a violent rampage. They said he traveled frequently between the South and New York, where he fathered a child in Brooklyn, and had been in the city earlier in the week. Brinsley, 28, had at least 19 arrests in Georgia and Ohio, spent two years in prison for gun possession and had a troubled childhood so violent that his mother was afraid of him, police said. He ranted online about police and the government and expressed “self-despair and anger at himself and where his life was,” Boyce said. Boyce said Brinsley's mother believed he had undiagnosed mental problems and may have been on medication at some point, but detectives were still trying to determine if he had a mental illness. On Saturday afternoon, Brinsley approached a squad car from behind in Brooklyn's Bedford-Stuyvesant neighborhood and fired four shots, killing Officers Rafael Ramos and Wenjian Liu. He then ran into a subway station and committed suicide. Hours earlier, Brinsley had shot and wounded his ex-girlfriend at her home outside Baltimore, then made threatening posts online, including a vow to put “wings on pigs” and references to the Brown and Garner cases. Baltimore-area police warned the New York department that Brinsley was in the city and bent on violence. But New York police were still getting the word out when Brinsley struck. The slayings dramatically escalated tensions that have simmered for months over police killings of blacks. The siege mentality was evident in several memos circulating among the rank and file at the 35,000-officer New York Police Department, the largest in the US. A union-generated message warned police officers that they should respond to every radio call with two cars — “no matter what the opinion of the patrol supervisor” — and not make arrests “unless absolutely necessary.” The president of the detectives union told members in a letter to work in threes when out on the street, wear bulletproof vests and keep aware of their surroundings. “Cowards such as yesterday's killer strike when you are distracted and vulnerable,” the letter read. Another directive warned officers in Newark, New Jersey, not to patrol alone and to avoid people looking for confrontations. At the same time, a memo from an NYPD chief asked officers to avoid fanning rage by limiting comments “via all venues, including social media, to expressions of sorrow and condolence.” New York City Police Commissioner William Bratton and Mayor Bill de Blasio attended Sunday Mass at St. Patrick's Cathedral, where Cardinal Timothy Dolan called for calm. Bratton later visited the families of both officers and laid flowers at a makeshift memorial at the site of the slayings. Hours before shooting the officers, Brinsley had shot and wounded his ex-girlfriend, Shaneka Thompson, at her home in Baltimore. Thompson's neighbor Yevette Seay told The Associated Press that Thompson's screams for help early Saturday jolted her out of bed. Seay called police as Thompson lay on the floor, clutching her stomach. Authorities have said Thompson is expected to survive. After leaving Baltimore, authorities said, Brinsley took a bus north to New York City and used Thompson's phone to write on Instagram: “They take 1 of ours, let's take 2 of theirs.” He ended the post with references to the Brown and Garner cases. — AP