A gunman has shot dead two police officers sitting inside a patrol car in New York before killing himself.
The head of the New York police said the men had been 'targeted for their uniform'.
The gunman ran into a nearby subway station where he then shot himself, officials say.
Earlier, the attacker shot and injured his ex-girlfriend and had posted anti-police messages on social media.
The mayor of New York, Bill de Blasio, said anyone who saw postings indicating a threat to the police should report them to the authorities. The "whole city was in mourning" after the shootings, he said.
NY police face intense scrutiny
The BBC reports the shootings come at a time when New York city police are facing intense scrutiny for their tactics.
Earlier this month, a grand jury did not indict any New York police department officer in the chokehold death of Eric Garner, a black man who died when white police officers tried to arrest him for selling cigarettes.
The decision sparked protests in New York and other cities across the US.
The officers were on duty in the Bedford-Stuyvesant area of Brooklyn when they were shot on Saturday afternoon.
Officers Liu Wenjin and Raphael Ramos were pronounced dead in hospital.
Bill Bratton, commissioner of the New York police department, said the officers had been shot with "no warning, no provocation - they were quite simply assassinated".
The killer's name was given as Ismaaiyl Brinsley, 28.
The gunman was a black man while the two police officers were Asian and Hispanic, police said.
Commissioner Bratton said the suspect had wounded his former girlfriend earlier on Saturday in Baltimore and had made posts from her Instagram account.
"This may be my final post," said one that included an image of a silver handgun.
Two officials told the Associated Press news agency that the gunman had also posted about shooting police in retaliation for the death of Mr Garner.
US Attorney General Eric Holder called the shootings an "unspeakable act of barbarism".
The Rev Al Sharpton, a prominent civil rights activist, said Mr Garner's family had had no connection to the gunman and he denounced the violence.
"Any use of the names of Eric Garner and Michael Brown [a black man shot by a white police officer in Ferguson, Missouri] in connection with any violence or killing of police, is reprehensible and against the pursuit of justice in both cases," Rev Sharpton said.