About Question enthuware.ocpjp.v7.2.1320 :
Moderator: admin
-
- Posts: 47
- Joined: Mon Apr 08, 2013 7:55 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1320 :
Is there some way to stop/kill a Thread from other Thread ?
I found the methods stop(), suspend() but they were Deprecated
I found the methods stop(), suspend() but they were Deprecated
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1320 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1320 :
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.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1320 :
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();
}
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1320 :
i see. Thanks a lot.
-
- Posts: 28
- Joined: Wed Dec 04, 2013 7:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1320 :
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,
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,
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1320 :
From http://docs.oracle.com/javase/7/docs/ap ... hread.htmltn1408 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,
In other words, it is the thread that calls this method is the one that goes to sleep.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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 13
- Joined: Sat May 16, 2015 12:38 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1320 :
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.
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.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1320 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1320 :
A nice explanation of interrupt(), isInterrupted(), interrupted() method here:
https://coderanch.com/t/237332/certific ... ted-method
https://coderanch.com/t/237332/certific ... ted-method
Who is online
Users browsing this forum: Bing [Bot] and 245 guests