The fact is that the flow from the first time correctly works and through the cycle I stop this flow. Then after some time I start the same thread back, but I get an error

IllegalThreadStateException: Thread already started.

Maybe I’m stopping wrong or just my logic is wrong. Help is needed.

SlideThread slideThread; 

and in onCreate () initialize ..

 slideThread = new SlideThread(); 

and then check

 if (video.getCategory().equals("advertising")) { if (slideTHread.isAlive){ slideThread.interrupt(); } } if (video.getCategory().equals("background")) { if (!slideThread.isAlive){ slideThread.start(); } } 

// ....

 public class SlideThread extends Thread { @Override public void run() { super.run(); final List<Banner> bannerList = GlobalData.loadBannerInfo(MainActivity.this); for (int i = 0; i < bannerList.size(); i++) { final File file = new File(Application.banner_path + bannerList.get(i).getName()); runOnUiThread(new Runnable() { @Override public void run() { mBannerView.setVisibility(View.VISIBLE); mBannerView.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath())); } }); try { Thread.sleep(15 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } } @Override public void interrupt() { super.interrupt(); runOnUiThread(new Runnable() { @Override public void run() { mBannerView.setVisibility(View.GONE); } }); } } 

and even after the completion of the stream, all the same after 15 seconds, the delay itself works.

Give me a clue what I'm doing wrong?

    1 answer 1

    The start() method can be called on the Thread object only once.

    Therefore, either embed the logic of start-ups inside the flow, or create and launch a new flow each time.

    • If every time I create a new thread and start it, can I finish it? - DevOma
    • I do not understand the question. In your code, did you complete it? - tse