2

As i have Written a Simple Java Program to call Thread . below is my code

public class ThreadPoolForParallelExec {

    public static void main(String args[]) {
        ExecutorService service = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            service.submit(new Task(i));
        }
        service.shutdown();
    }
}

final class Task implements Runnable {
    private int taskId;

    public Task(int id) {
        this.taskId = id;
    }

    @Override
    public void run() {
        myclient.intializeAndConnectRemoteMachine(taskId);
        Thread.currentThread().stop();
        Thread.currentThread().isInterrupted();
    }
}

However , I need to terminate the Executor or Thread . I tried Thread.currentThread().stop(); and Thread.currentThread().stop(); both didnt work :( could you please suggets .

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
user3377374
  • 61
  • 1
  • 5

6 Answers6

1

Generally speaking, to kill thread is a bad idea, and in fact, the latest Java specification deprecate that.

Instead, try to finish the thread gracefully within the thread itself. That is the consistent structure.

1

Just let the method end normally.

Then the Thread will be idle and the ExecutorService will shutdown afterwards.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

Never use Thread.stop. It has been deprecated:

From JLS:

This method is inherently unsafe. Stopping a thread with Thread.stop causes it to unlock all of the monitors that it has locked (as a natural consequence of the unchecked ThreadDeath exception propagating up the stack). If any of the objects previously protected by these monitors were in an inconsistent state, the damaged objects become visible to other threads, potentially resulting in arbitrary behavior. Many uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. If the target thread waits for long periods (on a condition variable, for example), the interrupt method should be used to interrupt the wait

The good way to do it is to have the run() of the Thread guarded by a boolean variable and set it to true from the outside when you want to stop it.

Make sure you had made the guarding boolean field volatile to make sure the reading thread sees changes from the writing thread.

Zeeshan
  • 11,851
  • 21
  • 73
  • 98
0

I think you should call to interrupt() and then wait Threads to finish. Then you could do any actions without having threads running.

Kasas
  • 1,216
  • 2
  • 18
  • 28
0

you can either use Thread.interrupt() or use volatile flag in run method and set it false when you want to stop thread.

@Override
public void run() {
    while (running) {
        try {
            ....
        } catch (InterruptedException e) {

            running = false;
        }
    }
}

while running is flag initialized as true.

for more details you can refer this link

Community
  • 1
  • 1
Ali
  • 1,480
  • 2
  • 20
  • 30
0

The documentation for version 1.5 says:

interrupt

public void interrupt()
Interrupts this thread.

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.

If this thread is blocked in an invocation of the
wait(), wait(long), or wait(long, int) methods of the
Object class, or of the join(), join(long), join(long,
int), sleep(long), or sleep(long, int), methods of this
class, then its interrupt status will be cleared and it
will receive an InterruptedException.

If this thread is blocked in an I/O operation upon an
interruptible channel then the channel will be closed,
the thread's interrupt status will be set, and the
thread will receive a ClosedByInterruptException.

If this thread is blocked in a Selector then the
thread's interrupt status will be set and it will
return immediately from the selection operation,
possibly with a non-zero value, just as if the
selector's wakeup method were invoked.

If none of the previous conditions hold then this
thread's interrupt status will be set.

Throws:
SecurityException - if the current thread cannot modify this thread
Marichyasana
  • 2,966
  • 1
  • 19
  • 20