1

I have a Thread (implements Runnable) from many branch officer call that thread with their branch code. I set up a name of their thread with branch code. The problems are...

  1. When an exception occurred in running thread - I can't stop that. And when try to make another thread with any name "ExceptionInInitializerError" or "OutOfMemoryError: Java heap space" comes
  2. "OutOfMemoryError: Java heap space" exception comes When 2 or more thread running at a time.
public MyRunnerClass {

    //This method called from many branch with their branch Code
    public void executeBranchProcess(String branchCode){
        Thread t = new Thread(new Exporter(branchCode);
        t.setName(branchCode);
        t.start();
    }
}

Thread Class here

public class Exporter implements Runnable{
    private String branchCode;

    public Exporter(String branchCode){
        this.branchCode = branchCode;
    }

    @Override
    public void run() {
        try {
            exportCorner();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void exportCorner() throws InterruptedException{
        try{
            //Some Process
        }catch(Exception e){
            // I want to close running thread here
            // I am using closeThread(this.branchCode), but not working
        }
    }


    static void closeThread(String branchCode) throws InterruptedException {
        Thread thread = null;
        for (Thread t : Thread.getAllStackTraces().keySet()) {
            if (t.getName().equals(branchCode))
                thread = t;
        }
        if (thread != null) {
            thread.interrupt();
            thread.join();
        }
    }
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Mr. Mak
  • 837
  • 1
  • 11
  • 25
  • 2
    I have no idea what you are asking. If an exception occurs within a thread's run method, then it simply dies (unless one is implementing some kind of endless loop that simply eats up exceptions instead of reacting to them). In other words: if your thread run hits an exception, just make sure that your code "returns" from that method (for example after logging the exception) and you should be good. – GhostCat Jul 19 '16 at 12:27
  • Possible duplicate of [How to stop a thread created by implementing runnable interface?](http://stackoverflow.com/questions/10630737/how-to-stop-a-thread-created-by-implementing-runnable-interface) – Tamas Rev Jul 19 '16 at 12:28
  • 2
    Use an `ExecutorService`, and cancel the `Future` that is returned when you submit a task to it. Also, you can use `Callable` instead of `Runnable`, whose `call()` method allows you to throw checked exceptions instead of handling them explicitly. – Andy Turner Jul 19 '16 at 12:31
  • 2
    Just catch the Exception as you do, treat it the way you want (write logs, print it in the console), and let it go. The thread will terminate itself, as he reached the end of it's code. – Ephi Jul 19 '16 at 12:32
  • 2
    Also: why do you think you need to call `closeThread`? You *know* which thread is running with `this.branchCode` - it's `Thread.currentThread()` (unless you are starting multiple threads for the same branch code, which is.. yikes); and effectively calling `Thread.currentThread().join()` is just silly (it blocks the current thread, waiting for the current thread to finish). – Andy Turner Jul 19 '16 at 12:36

1 Answers1

1

You face multiple problems here:

  1. You cannot join a thread in itself. Thread.join() waits until the thread dies. But if you call it from the thread you want to stop, it just waits forever.

  2. To stop a thread you simply have to return from its run() method. In your case, just add return in your catch clause instead of calling closeThread().

  3. It seems that you have some memory problems. Either whatever you do in exportCorner() uses alot of memory or you create to many threads at once. As Andy Turner has mentioned in the comments, it might be usefull to use an ExecutorService to handle your Runnables. This may help you managing your threads and ensure a limited thread count.

kaetzacoatl
  • 1,419
  • 1
  • 19
  • 27