[Solved] Wait Till Previous Method Completes [6.0.0]

Sponge API: 6.0.0-20170208.122041-38
Sponge Build: 1.11.2-2226-6.0.0-BETA-2129
Forge Build: forge-1.11.2-13.20.0.2226-universal
**Java Version:**1.8.0_121-b13

Hello, so I have the code below and it works, but the problem is that my TaskBuilder executes before both of the teleportPlayersToArena method calls finish executing. Is there a way to execute the TaskBuilder after the two method calls are done? Thanks!

public static void beingTDM(String arenaName) {

	List<Player> contestants = convertObjectContestantsToPlayers(arenaName);
	
	Collections.shuffle(contestants);
	
	defineBothTeams(contestants);
	
	teleportPlayersToArena(arenaName, teamAContestants, "Team A Spawnpoints");
	
	teleportPlayersToArena(arenaName, teamBContestants, "Team B Spawnpoints");
	
	Sponge.getScheduler().createTaskBuilder().interval(1, TimeUnit.SECONDS).delay(1, TimeUnit.SECONDS)
	.execute(new TeamDeathmatchTimer(contestants, teamAName, teamBName, teamAContestants, teamBContestants)).submit(Main.Main.dreadzone);

}

Figured it out. Adding synchronized to the method fixed my problem.

private static synchronized void teleportPlayersToArena(Object arenaName, List<Player> teamContestants, String teamName) {
	
	Set<Object> arenaTeamSpawnNames = ArenaConfigUtils.getArenaTeamSpawnpoints(arenaName, teamName);
	
	for(Object arenaSP:arenaTeamSpawnNames){
	
		for(Player player: teamContestants){
			
			player.setLocationAndRotation(ContestantConfigUtils.sendContestantToArenaLocation(arenaName, teamName, arenaSP), 
					ContestantConfigUtils.sendContestantToArenaLocationRotaton(arenaName, teamName, arenaSP).add(0.5,0,0.5));
		}
	}
}

Well, what’s the sendContestantToArenaLocationRotaton method contain?

It gets the saved head rotation from my configuration file.

public static Vector3d sendContestantToArenaLocationRotaton(Object arenaName, Object teamName, Object spawnName){

	CommentedConfigurationNode targetNode = UnversalConfigs.getConfig(ArenaConfig).getNode("Arena", arenaName,"Spawnpoints",teamName,spawnName,"Transform");
	
	int tx = targetNode.getNode("X").getInt();
	int ty = targetNode.getNode("Y").getInt();
	int tz = targetNode.getNode("Z").getInt();

	return new Vector3d(tx, ty, tz);
}

EDIT: I moved this part to the sendContestantToArenaLocation method call, as originally intended.

Reason I ask is, something in that method call, or its method calls, or its method calls, etc., would have to do something asynchronous. The synchronized keyword only means that only one thread can call the code at a time.

Adding that keyword seems to do the trick.

I’m just saying I don’t know why it would, because it doesn’t actually prevent any previously asynchronous bits of code from remaining asynchronous. synchronized code prevents more than one thread from accessing it simultaneously, but it doesn’t prevent the method itself from spawning more threads.