0

I have Character and a Monster, and want to simule a battle. To do so, I created two threads, one with the character's attack, and another with the monster's attack. Character and monster has different attack speed, so their threads "wait" time will be different. My method looks like this (I summed up the method):

public void attack(){

//Character's attack thread
Thread characterAttackThread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            while(character.getHp()>0 && monster.getHp()>0){
                int damage = character.getDamage();
                monster.setHp(monster.getHp() - damage);
                Thread.sleep(character.getSpeed());
            }
            if(character.getHp()<=0){
                updateScreen("Character died"); //shows message to user
            }else{
                updateScreen("Monster died"); //shows message to user
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

//Monster's attack thread
Thread monsterAttackThread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            while(character.getHp()>0 && monster.getHp()>0){
                int damage = character.getDamage();
                monster.setHp(monster.getHp() - damage);
                this.wait(character.getSpeed());
            }
            Thread.sleep(monster.getSpeed());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

characterAttackThread.start();
monsterAttackThread.start();
}

But when I reach the updateScreen("Message"); I get the error

Only the original thread that created a view hierarchy can touch its views.

What can I do to get the problem solved?

Thanks in advance!

IIRed-DeathII
  • 1,117
  • 2
  • 15
  • 34
  • 1
    Possible duplicate: http://stackoverflow.com/questions/26590542/java-lang-illegalmonitorstateexception-object-not-locked-by-thread-before-wait –  Aug 12 '15 at 20:08
  • *Character's attack **thread*** thread? thread? it is not thread ...you are doing all in one thread ... the main/UI thread ... posting Runnable to main/UI thread handler is not a new thread ... this code makes no sens as your "threads" will not run at the same time ... – Selvin Aug 12 '15 at 20:17
  • You havnt created two threads...where exactly do you think you are doing so? – JShell Aug 12 '15 at 20:22
  • A Runnable is NOT a thread. It is an object with a chunk of code that can be run by calling the run() method. runOnUiThread posts the Runnable to a queue where the main thread's Looper will call run() on it once it gets pulled from the head of the queue. – Michael Krause Aug 12 '15 at 20:38
  • corrected the question. sorry for the misunderstanding – IIRed-DeathII Aug 12 '15 at 22:48

1 Answers1

1

You cannot modify the android toolkit from any other thread apart from UI thread

This link will give you clarifications :

http://developer.android.com/guide/components/processes-and-threads.html

You may do one of the follow:

  1. Use AsyncTask and update UI from onPostUpdate() function
  2. Write a handler in UI thread and postmessage to it from your thread
  3. Use the following code in your thread:

    MainActivity.this.runOnUiThread(new Runnable() {
    

    public void run() {

        Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
    
    }
    

    });

Hope this helps

Sushil
  • 8,250
  • 3
  • 39
  • 71