0

As for every Android-dummy, also I run into the FATAL EXCEPTION

java.lang.IllegalThreadStateException: Thread already started

Scrolling forums and questions, I have seen that a Thread cannot be started twice. I have seen answers like these:

"It's not a good Idea to start a Thread more than once."

"A Thread can be called just one time."

But nobody says WHY a Thread can be called just once. It sounds so weird to me that there is an Android function that is born to be called only one time.

Why a Thread can be called just once?

I am developing an App in which I have to call an animation every time I tap the screen. I thought that a Thread could be the most suitable tool for me. Am I wrong?

UPDATE

Maybe the question is not clear.

I am not asking why a Thread cannot be restarted while it is alive.

I am asking why a Thread that I have run but now is finished (so it is not alive) cannot be called a second time.

This is my code fully simplified. The Thread is called the first time at onCreate and the second time at onClick, so when I tap the image. At the second call, I can see from Android Monitor that the Thread is not Alive, though it not starts anyway.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        chiamaDatabase();
        RESET.start();

        ImageView image = (ImageView) findViewById(R.id.imageView2);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!RESET.isAlive()) {
                    RESET.start();
                }
            }
        });
    }

    public Thread RESET = new Thread() {
        public void run() {
            final ImageView image = (ImageView) findViewById(R.id.imageView2);
            Random random = new Random();
            int n = random.nextInt(2);
            image.setX(320+n);
            image.setY(400+n);
        }
    };
Community
  • 1
  • 1
  • 1
    post your relevant code... – rafsanahmad007 Apr 30 '17 at 12:08
  • 2
    "I thought that a Thread could be the most suitable tool for me" -- standard Android animations do not usually need threads. "But nobody says WHY a Thread can be called just once" -- because that is the way that they implemented that Java class, ~20 years ago. Questions asking "why did Developer X make Decision Y" do not work well on Stack Overflow. Typically, only Developer X can answer the question, and Developer X may not be helping out here. Often, anyone else can only offer opinions. – CommonsWare Apr 30 '17 at 12:15
  • @rafsanahmad007 done :) –  Apr 30 '17 at 12:46

1 Answers1

0

You should have a look at what a Thread is: https://en.wikipedia.org/wiki/Thread_(computing)

It makes no sense to start a thread which is already running, therefore, languages have to chose what to do when somebody tries to start a thread which is already running:

  1. Ignore the command
  2. Throw an error

Java chose to do the second, which I think is the best thing to do.

If you really have to start the thread again, you won't be able to in Java. Check this link describing the lifecycle of threads in Java: http://www.roseindia.net/java/thread/life-cycle-of-threads.shtml

You can also refer to this other StackOverflow thread: How can a dead thread be restarted?

Community
  • 1
  • 1
Mikel Pascual
  • 2,202
  • 18
  • 27
  • thanks for the answer. Sorry but my question wasn't so clear: I was talking about a Thread that I have run but now is not running anymore and for Android it is `RESET.isAlive()=false`. I have updated the question to be more clear. –  Apr 30 '17 at 12:46