Page 1 of 1

About Question enthuware.ocpjp.v7.2.1320 :

Posted: Thu May 09, 2013 6:33 pm
by renatumb
Is there some way to stop/kill a Thread from other Thread ?
I found the methods stop(), suspend() but they were Deprecated

Re: About Question enthuware.ocpjp.v7.2.1320 :

Posted: Sat May 11, 2013 6:27 am
by admin
Nope :)
The recommended approach is to interrupt a thread, which sets the interrupted flag of the thread to true. Your thread code should periodically check for this flag and stop whatever it is doing on its own.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1320 :

Posted: Sat Mar 01, 2014 4:56 am
by icepeanuts
I don't fully understand the explanation "if the thread on which interrupt() is called, is blocked in an invocation of the wait(...), join(...), or sleep(...), methods of this class, then its interrupt status will be cleared and that thread (not the caller of interrupt) will receive an InterruptedException." Could you please give me an example? Thanks.

Re: About Question enthuware.ocpjp.v7.2.1320 :

Posted: Sat Mar 01, 2014 5:06 am
by admin
Here is an example:

Code: Select all

class Waiter extends Thread{
    public void run(){
        synchronized(this){
            try{
                System.out.println("Waiter about to wait...");
                this.wait();
            }catch(Exception e){
                e.printStackTrace();
            }
            System.out.println(this.isInterrupted());
        }
    }
}
public class InterruptTest {
    public static void main(String[] args) throws Exception{
        Waiter w = new Waiter();
        w.start();
        Thread.sleep(1000);
        System.out.println("Interrupting waiter");
        w.interrupt();
        
    }
}

Re: About Question enthuware.ocpjp.v7.2.1320 :

Posted: Sun Mar 02, 2014 12:25 am
by icepeanuts
i see. Thanks a lot.

Re: About Question enthuware.ocpjp.v7.2.1320 :

Posted: Fri Apr 11, 2014 5:21 pm
by tn1408
Just a quick question:

the call Thread.sleep(1000) will put which thread to sleep? The main thread or the a thread?
Does it depend on which thread is actually executing at the time?

Thanks

Tony,

Re: About Question enthuware.ocpjp.v7.2.1320 :

Posted: Fri Apr 11, 2014 8:22 pm
by admin
tn1408 wrote:Just a quick question:

the call Thread.sleep(1000) will put which thread to sleep? The main thread or the a thread?
Does it depend on which thread is actually executing at the time?

Thanks

Tony,
From http://docs.oracle.com/javase/7/docs/ap ... hread.html
static void sleep(long millis)
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.
In other words, it is the thread that calls this method is the one that goes to sleep.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1320 :

Posted: Mon Sep 28, 2015 11:40 pm
by pbonito
Reading javadoc

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.


It seems that a different thread may not call interrupt on another thread. In this case it seems that thread main is trying to interrupt thread a and I would expect a SecurityException.

Re: About Question enthuware.ocpjp.v7.2.1320 :

Posted: Tue Sep 29, 2015 12:35 am
by admin
That is only if a SecurityManager is installed and it explicitly denies access to the target thread. But that is a special situation. Not a regular one that you need to consider for the purpose of this exam.

Re: About Question enthuware.ocpjp.v7.2.1320 :

Posted: Fri Dec 30, 2016 3:12 pm
by jagoneye
A nice explanation of interrupt(), isInterrupted(), interrupted() method here:
https://coderanch.com/t/237332/certific ... ted-method