2

I'm doing a cours Exersice about Threads. my question is how to terminate a Thread that wrap a function (and recives maximu time to be alive) when the function finished and return the result in java.

this is an Example of a function:

public static boolean isPrime(long n){
    boolean ans=true; 
    if(n<2)throw new RuntimeException("ERR: the parameter to the isPrime function must be >1 (got "+n+")!");
    int i=2;  double ns=Math.sqrt(n) ;  
    while(i<=ns&&ans){ 
        if (n%i==0) ans=false; 
        i=i+1;  
    }  
    if(Math.random()<Ex4_tester.ENDLESS_LOOP)while(true); 
    return ans;  
}

this is my code and I'm not sure if it works:

public class Ex4 {

    private class myThread extends Thread {
        private long n;
        private boolean ans;
        private boolean finish = false;
        public myThread(Long n) {
            this.n = n;
        }

        @Override
        public void run() {
            ans = Ex4_tester.isPrime(n);
            finish = true;
            System.out.println(ans);

        }
    }

    @SuppressWarnings("deprecation")
    public boolean isPrime(long n, double maxTime)  throws RuntimeException, InterruptedException {
        myThread check = new myThread(n);
        check.start();

        try {
            check.join((long)(maxTime * 1000));     
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }

        if ((check.isAlive()) && (check.finish)) {
            check.stop();           
        }
        return check.ans;
    }
}
CodeWalker
  • 2,281
  • 4
  • 23
  • 50
Gabriel
  • 121
  • 1
  • 1
  • 8
  • 1
    @AlexisKing: I don't think this is a dupe of that. The OP here seems to be asking how to terminate a thread when its `run` method completes; so the answer is "you don't need to do anything special, that just happens automatically". – ruakh Jan 24 '15 at 18:49
  • @ruakh Yes, you're right, I misinterpreted the question. – Alexis King Jan 24 '15 at 18:51
  • @ruakh, he is asking how to kill a thread after a set time, and his code is using the deprecated and unsafe `stop()` call. I think he is trying to kill a thread. – RealSkeptic Jan 24 '15 at 18:51
  • I think you'll have much better luck if you can use a [`Future`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html). – Elliott Frisch Jan 24 '15 at 18:54

1 Answers1

0

one way, handle InterruptedException, use Thread.interrupt(),

more to read: http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html

more on stopping thread without Thread.stop():

https://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

user2649908
  • 557
  • 7
  • 15
  • If you interrupt a thread, it doesn't cause an `InterruptedException`, unless the thread is blocked on a method that expects it, such as `sleep()`. – RealSkeptic Jan 24 '15 at 19:00
  • @RealSkeptic: yes, that's what the interrupt status flag mentioned in the tutorial is for. i have an example [here](http://stackoverflow.com/a/5915306/217324) – Nathan Hughes Jan 24 '15 at 19:37