0
public class Letters extends Thread implements Iterable<Thread>
{
    private String chars;

    private Thread[] threads;
    int index = 0;

    public Letters(String chars)
    {
        this.chars = chars;
        this.threads = new Thread[chars.length()];

        for (int i = 0; i < threads.length; i++)
        {
            int finalI = i;
            Thread thread = new Thread()
            {
                @Override
                public void run()
                {
                    while (!Letters.this.isInterrupted())
                    {
                        System.out.print(chars.charAt(finalI));
                        try
                        {
                            Thread.sleep((int) (Math.random() * 2000));
                        }
                        catch (InterruptedException e)
                        {
                            throw new RuntimeException(e);
                        }
                    }
                }
            };

            threads[finalI] = thread;
        }
    }

    @Override
    public void run()
    {
        for (Thread thread : threads)
        {
            thread.start();
        }

        while (true)
        {
            if (this.isInterrupted())
                return;
        }
        
    }

    Iterator<Thread> threadsIterator = new Iterator<Thread>()
    {
        @Override
        public boolean hasNext()
        {
            return index < threads.length;
        }

        @Override
        public Thread next()
        {
            return threads[index++];
        }
    };

    @Override
    public Iterator iterator()
    {
        return threadsIterator;
    }
}

The Letters class object is created in main method and there is interrupted:

public static void main(String[] args)
{
    Letters letters = new Letters("ABCD");

    for (Thread t : letters)
        System.out.println(t.getName() + " starting");

    letters.start();
    try
    {
        Thread.sleep(5000);
    }
    catch(InterruptedException ignore)
    {

    }
    letters.interrupt();
    System.out.println("\nProgram completed.");
}

My question is about this part:

@Override
    public void run()
    {
        for (Thread thread : threads)
        {
            thread.start();
        }

        while (true)
        {
            if (this.isInterrupted())
                return;
        }
        
    }

Why if i use Thread.interrupted() instead of isInterrupted() it doesn't intterupt the parent threads that was started by this thread? In Java, how are sub-threads closed when the parent thread finishes its work?

wydra__
  • 25
  • 4
  • Re, "...sub-threads...parent thread..." Java does not keep track of which threads in a program created which other threads. Every thread belongs to a[`ThreadGroup`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ThreadGroup.html), and there is a parent/child relationship between thread _groups,_ but there is no such relationship between individual threads. If it is meaningful for your program to keep track of which thread created which other thread, then it is up to you to write the code for that. – Solomon Slow Jun 22 '23 at 15:53
  • 2
    BTW difference between `interrupted()` and `isInterrupted()` (despite first being `static` while second not): `interrupted()` clears the *interrupted status*, while `isInterrupted()` does not (the Thread continues being *interrupted*.) See [documentation](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/Thread.html#interrupted()) – user16320675 Jun 22 '23 at 16:22
  • 1
    Aside from main question, please note that `class ... extends Thread` is usually code smell. You can think of difference between Thread and Runnable as difference between Worker and Task it needs to do. Purpose of your class is most likely NOT to modify/specify *how* thread (worker) should work, but only *what* it should do (what task it needs to handle). So our default choice should be `class ... implements Runnable`. – Pshemo Jun 22 '23 at 16:42
  • You may also be interested in [Java: Difference in usage between Thread.interrupted() and Thread.isInterrupted()?](https://stackoverflow.com/q/1904072), and ["implements Runnable" vs "extends Thread" in Java](https://stackoverflow.com/q/541487). And don't read *only* currently top/accepted answers. Often even better answers appear years later after question was asked so if you have time I encourage you to read also other answers. – Pshemo Jun 22 '23 at 16:44

0 Answers0