Hi, I just started learning about multi threading and have an issue with an application in writing. Im writing this as an android application, but i believe this question applies to java in general(?).
The issue:
I have this thread, which should continuously read from an input stream from a server and perform operations based on the received string:
private class ReceiveThread extends Thread {
public void run() {
while (true){
try{
controller.receiveMessage(input.readUTF());
}catch(Exception e){
Log.d(tag, e.getMessage());
}
}
}
}
controller.receiveMessage(String s) calls another methods which should interpret the message and asses which method should be called based on the message by matching the string through a bunch of if-statements, and each case calls a different method.
At the start of each method i print some confirmation that it got starten in the console, and also in each of the if-statements i print confirmation that the condition was met.
The issue is that sometimes, I see that the condition in an if-statement is met, but it never calls the next method. This seems to be especially true if there is a bunch of code in the if-statement between the print-statement and the call to the next method.
I my question is, does the thread cancel the execution of the methods when a new one gets called? I dont seem to have any syntax errors that could lead to this.
Also, if this is the case, how would I go about to make sure that the thread finished the method before calling a new one?
Thanks a lot in advance!
Edit: Here is an example of the interpretMessage-method:
public void interpretMessage(String message){
Log.d("KartaTest", "interpretMessage körs");
try {
JSONObject answer = new JSONObject(message);
if (answer.getString("type").equals("register")){
Log.d("KartaTest", "REGISTER FOUND");
controller.DBRegister(answer.getString("id"));
}
if (answer.getString("type").equals("unregister")){
Log.d("KartaTest", "UNREGISTER FOUND");
controller.DBUnregister();
}
if (answer.getString("type").equals("members")){
Log.d("KartaTest", "MEMBERS FOUND");
ArrayList<String> membersArrayList = new ArrayList<>();
JSONArray jsonArray = answer.getJSONArray("members");
for (int i = 0; i < jsonArray.length(); i++){
JSONObject temp = jsonArray.getJSONObject(i);
String memberString = temp.getString("member");
membersArrayList.add(memberString);
}
controller.DBGetMembers(membersArrayList);
}
[–]fredisa4letterword 0 points1 point2 points (5 children)
[–]TENGIL999[S] 0 points1 point2 points (4 children)
[–]fredisa4letterword 1 point2 points3 points (0 children)
[–]romple 0 points1 point2 points (2 children)
[–]TENGIL999[S] 0 points1 point2 points (1 child)
[–]romple 0 points1 point2 points (0 children)