About Question enthuware.ocpjp.v7.2.1320 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
renatumb
Posts: 47
Joined: Mon Apr 08, 2013 7:55 pm
Contact:

About Question enthuware.ocpjp.v7.2.1320 :

Post by renatumb »

Is there some way to stop/kill a Thread from other Thread ?
I found the methods stop(), suspend() but they were Deprecated

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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();
        
    }
}
If you like our products and services, please help us by posting your review here.

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

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

Post by icepeanuts »

i see. Thanks a lot.

tn1408
Posts: 28
Joined: Wed Dec 04, 2013 7:57 pm
Contact:

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

Post 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,

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

pbonito
Posts: 13
Joined: Sat May 16, 2015 12:38 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye »

A nice explanation of interrupt(), isInterrupted(), interrupted() method here:
https://coderanch.com/t/237332/certific ... ted-method

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 245 guests