JOHANNESBURG — The World Cup ends Sunday, and South Africa, like some cartoon figure with an angel above one shoulder and the devil atop the other, will revel in its successful hosting of the tournament while bracing for possible violence aimed at the impoverished immigrants in its midst.

Related

A military presence in Diepsloot, where threats have raised the specter of attacks on immigrants after the World Cup ends

For months, threats have coursed through virtually every township and squatter camp, with warnings that once the final whistle blows, the tourists leave and the world looks away, vuvuzelas and banners will be replaced by torches and panga knives as attacks begin against Zimbabweans, Mozambicans and others.

“They tell us that by Sunday we must go or we will see blood, plenty blood,” said Precious Ncube, 25, a Zimbabwean living in a township near Pretoria.

Many in the government regard these threats as rumors inflated into hysteria through repetition. Nevertheless, security forces are on “high alert,” Police Minister Nathi Mthethwa said last week. The military has already demonstrated its muscle in some presumed hot spots. Human rights groups and the Nelson Mandela Foundation have issued pleas for calm.

On Friday, as some immigrants fled the country, the powerful Congress of South African Trade Unions, or Cosatu, urged the government to open stadiums for people who might need havens, saying, “Surely, given the billions of rands we just spent on the World Cup, where we did not spare any expense, we cannot now risk people’s lives.”

There is good reason for apprehension.

During two weeks in May 2008, at least 60 people, mostly from nearby African nations, were killed by mobs. The violence, stomped out in one place only to reappear in others, occurred in Johannesburg, Cape Town, Durban and elsewhere. About 35,000 people were hounded from their homes.

These paroxysms of burning and looting were called “xenophobic riots,” and the defining image, published throughout the world, was that of a Mozambican man in the final moments of his life after being set ablaze.

The government was slow to intervene. Thabo Mbeki, then the president, spoke out only belatedly. Thousands of the uprooted slept on the floors of police stations, churches and community halls. South African volunteers, appalled by the attacks, fed the hungry and organized shelters.

Fungai Makota, a 36-year-old Zimbabwean woman who owns a tiny grocery in a Johannesburg township, remembered being chased from her shop in 2008. Her entire inventory was looted, and her husband, a Mozambican, suffered serious head wounds after a furious pummeling.

Like so many others, she is trying to figure out why these threats have resurfaced now and how much danger she is in. Should she stay or go, continue to sell her groceries or hide them?

“So many people tell me, ‘You must return where you came from; your time in South Africa is up,’ ” Ms. Makota said. “But I have lived here eight years. My life is here now.”

During the past month, this country has shown its best side to the world. Leaders from both government and business have declared that South Africa has successfully “rebranded” itself, recasting an image tarnished by AIDS, poverty and corruption into one of geniality, prosperity and competence.

Part of the nation’s charm has been its spirit of pan-African brotherhood. When South Africa’s team was eliminated from the World Cup, loyalties turned in lockstep to Ghana, the only team from the continent to advance.

But many citizens here, particularly the poor who cobble their hovels from rusty metal and scrap wood, resent the estimated five million foreigners who have crossed the border looking for work in Africa’s largest economy. About a third of this nation’s workers are unemployed, and immigrants are commonly blamed for taking their jobs or robbing their homes.

“These foreigners have no IDs, no papers, and yet they get the jobs,” said Ephraim Magoele, 26, an unemployed South African. “They are willing to work for 15 rand a day,” about $2. “When I need money, I have to call my mother and ask her for some money from her pension. Why should this be?”

Such resentment is easy to heat into hatred — and easy to exploit.

The Forced Migration Studies Program at the University of the Witwatersrand in Johannesburg, which has examined the nation’s continuing xenophobic impulses, concludes that local political leaders often instigate the violence as a means to advance personal agendas. Business owners sometimes support it to wipe out competition. Looters certainly profit.

Most South Africans deplored the 2008 attacks. In fact, many in the shack settlements defended their foreign neighbors or offered places to hide. Even now, it is hard to find people who say they will take part in a purge of immigrants — but not so hard to find those who would support it.

“It is up to the foreigners whether we are required to beat them or kill them; whatever, they must go,” said Johannes Thabompooa, 41, who claimed he lost his job four years ago and had not worked since.

The timing of the threatened attacks is decidedly bizarre, as if the perpetrators consider it appropriate to kill foreigners, but wrong to interfere with the World Cup in doing so. In an editorial, the South African newspaper The Mail and Guardian called that kind of thinking a “calculated sort of pseudopatriotism.”

The local news media’s persistent reporting of the threats has added to the fear. On Thursday, prominence was given to an episode near Cape Town in which a Zimbabwean man, Reason Wandi, was thrown from a train. He said his attackers condemned him as a foreigner as they opened the door.

In many settlements, there is now an expectation of violence, and news of one outbreak may well become the spark for others. There is always plenty of tinder amid the close quarters — disputes about housing and crime, about water and toilets, about winnings in a card game, and about who sleeps with whom.

During any unrest, foreign shopkeepers are almost reflexively singled out. Shahid Butt, a Pakistani, runs the Vuwani Corner, a small but busy grocery in Diepsloot, a township north of Johannesburg. His store was looted in 2008 and again six months later in a second round of attacks.

Weeks ago, he stopped replenishing his inventory, and said that he intended to remove merchandise during the weekend as a precaution.

“Some guys come in here talking nonsense and say, ‘We will show you after the World Cup,’ ” Mr. Butt said defiantly, shaking his head. “Show me what?”

He put an apple down on the counter.

“If you want to eat this food, you have to grab it with your hand,” he said as a line of customers waited. “This apple will not jump into your mouth. I came here nine years ago and I have a business. Why don’t the local people have a business? Because you can’t get anything if you sit at home.”