14.05.2016, 14:43
@valych: You can use multiple threads if you want to save and load data from a database server, as long as the database server supports multithreading. As soon as you try to use any kind of internal function of the SA:MP server, you will have to let that run on the main SA:MP thread, because otherwise, the server will crash.
Here is a example:
Here is a example:
PHP код:
Thread fetchThread = new Thread(() -> {
//Fetching my data:
Map<String, String> locationData = new HashMap<>(); //example
try {
Thread.sleep(5000); //simulate workload
locationData.put("Player1", "Germany"); //example data
locationData.put("Player2", "Italy"); //example data
locationData.put("Player3", "Poland"); //example data
} catch (InterruptedException e) {
e.printStackTrace();
}
Shoebill.get().runOnSampThread(() -> {
//Do whatever you want to do with the data if it has something to do with SA:MP
Player.getHumans().stream().filter(player -> locationData.containsKey(player.getName())).forEach(player -> {
player.sendMessage(Color.GREEN, "you are from " + locationData.get(player.getName()));
});
});
});
fetchThread.start(); //Start thread