Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Interrupting a thread from a method

Posted on November 11, 2022 By

Solution 1 :

worker.cancel was called in the main thread. Therefore, Thread.currentThread() inside the cancel method returns the main thread and therefore the interrupt method was called by the main thread. You are not interrupting the worker thread in this case.

Problem :

I want to interrupt a thread with interrupt() method, however I experience a behaviour I cannot explain.

As indicated in the code, calling the worker method cancel() from MainActivity can’t interrupt the thread, where calling workerThread.interrupt() can.

If the worker calls cancel() by itself it’s interrupting.

Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.

I thought it might be a security exception, cause the UIThread tries to interrupt the workerThread, but its not thrown. And if, it should also be thrown when workerThread.interrupt() is called?

Worker.java

package com.example.threadapp;
public class Worker implements Runnable {

    public void cancel() {
        Log.d("Threading", "call Thread.currentThread().interrupt()");
        try {
            Thread.currentThread().interrupt();
        } catch (SecurityException e) {
            e.printStackTrace();
            Log.d("Threading", "SecurityException while interrupting");
        }

    }

    @Override
    public void run() {
        //
        while(!Thread.currentThread().isInterrupted()) {
            Log.d("Threading", "Worker alive");
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
                cancel();
            }
            //cancel(); //interrupts thread
            //Thread.currentThread().interrupt(); //interrupts thread

        }
        //
        Log.d("Threading", "Worker Thread closing");
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Worker worker;
    private Thread workerThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        worker = new Worker();
        workerThread = new Thread(worker);
        workerThread.start();

        (new Handler(Looper.getMainLooper())).postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.d("Threading", " call worker.cancel()");
                worker.cancel(); //doesn't interrupt thread
                //workerThread.interrupt(); //interrupts thread
            }
        }, 10000);
    }
}

Comments

Comment posted by Seven

But isn’t workerThread.interrupt() also called from the main thread?

Comment posted by Loremar Marabillas

The interrupt method toggles a flag variable inside the worker thread. So when you call isInterrupted, it returns that value. Therefore it does not matter where you’re calling it. On the other hand, when you call mainThread.interrupt it toggles the flag variable of the main thread. isInterrupted will always return false in this case since it does not know the value from the main thread.

READ  [FIXED] android - Kotlin DatePicker Pop-Up doesn't work inside fragment : WindowManager$BadTokenException: Unable to add window -- token null is not valid;
Powered by Inline Related Posts

Comment posted by Seven

If it is only a flag, and it doesn’t matter from where it is set, why does ( called from MainThread) worker.cancel() won’t interrupt the thread and workerThread.interrupt(); will ?

Comment posted by Loremar Marabillas

Remember that the while loop is running on the worker thread, right? That happened when you called workerThread.start(). So when you call Thread.currentThread.isInterrupted, you’re actually calling workerThread.isInterrupted. That method asks for the value of the variable inside workerThread. That variable was changed when you called workerThread.interrupt. So it does not matter if you call it in the main thread, you will be able to change that variable. When you call mainThread.interrupt which happened when you call worker.cancel, that variable which was needed by isInterrupted didn’t change

Comment posted by Loremar Marabillas

Instead it changed the variable inside the mainThread. Remember, what was asked was the variable inside the workerThread. You can use Thread.currentThread.getName if you want to check which thread you are in. You’ll see that the code inside cancel method is indeed running in the main thread.

Android Tags:android, java, multithreading

Post navigation

Previous Post: [FIXED] Is it possible to switch from Start state to End state in android MotionLayout using Java/Kotlin?
Next Post: [FIXED] android – PHP / SQL: cannot save image at server but successful save image link at SQL database

Related Posts

[FIXED] android – Unity requires access to Huawei HMS SDK, package name must end with .HUAWEI Android
[FIXED] android – encrypt password in flutter Android
[FIXED] android – Adding multiple files to androidManfist.xml Android
[FIXED] java – Integer cannot be cast to Long Android
[FIXED] performance – Libgdx & Android: Playing multiple sounds fast causes game to freeze/stutter Android
[FIXED] android – Will the OS ever kill a started service and call `Service.onDestroy`? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme