how to break a thread in another class

Discussion in 'Java' started by aleel, Apr 25, 2004.

  1. aleel

    aleel Guest

    I am a beginner .Thank for your help !
    I constructed three classes ManyThreadsTest,Timer and
    Calculator.class Timer and Calculator implements the Runable interface
    ,class ManyThreadsTest
    includes two static variables t1 and t2,then in the static field of
    ManyThreadsTest announces :
    static {
    t1=new Thread (new Timer());
    t2 =new Thread (new Calculator());
    }
    then in the main function as follows :
    public static void main(String[] args) {

    t1 = new Thread(new Timer());
    t2 = new Thread(new Calculator());
    t1.start();
    t2.start();
    while(true)
    try {
    Thread.sleep(100);
    }
    catch (InterruptedException ex) {
    }
    }
    }
    and the run function of the Calculator as follows :
    public void run() {

    for (int i = 0; i <= 5000; i++) {
    String str = "a prime number!";
    if (isPrimeNum(i)) {
    System.out.println(i + " is " + str);
    }
    else {
    System.out.println(i + " is not " + str);
    }
    }
    ManyThreadsTest.t1.destroy();
    char c;
    try {
    c = (char) System.in.read();
    if(c=='q')
    System.exit(0);
    } catch (IOException e) {

    e.printStackTrace();
    }


    }
    the class Timer is used to record time , and the class Calculator
    is used to judge a number which is lower than 10000 is a prime number
    or not ,I want to break the Thread t1(used to record time )after the
    Calculator finished its work .so I writed
    "ManyThreadsTest.t1.destroy();" in the run function of Calculator
    class .however, I found the Thread t1 continued after the destroy ()be
    executed ,and the screen showed the time at all times.I wanted
    originally to input the char "q" to make the program leave .but I
    cannot do so now .how to solve the problem ?
     
    aleel, Apr 25, 2004
    #1
    1. Advertising

  2. aleel

    chris Guest

    aleel wrote:

    > the class Timer is used to record time , and the class Calculator
    > is used to judge a number which is lower than 10000 is a prime number
    > or not ,I want to break the Thread t1(used to record time )after the
    > Calculator finished its work .so I writed
    > "ManyThreadsTest.t1.destroy();" in the run function of Calculator
    > class .however, I found the Thread t1 continued after the destroy ()be
    > executed ,and the screen showed the time at all times.I wanted
    > originally to input the char "q" to make the program leave .but I
    > cannot do so now .how to solve the problem ?


    You would do better to use the t1.interrupt(), having first ensured that t1
    is in a nice long Thread.sleep(). The online documentation(*) for destroy()
    says "this method is not implemented", and just as well too.

    (*) For JDK 1.4.2 anyway. But I think this documented non-implememtation
    goes back a long way.

    --
    Chris Gray
    /k/ Embedded Java Solutions
     
    chris, Apr 25, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sayoyo
    Replies:
    3
    Views:
    1,175
    sayoyo
    Feb 16, 2004
  2. Nafai
    Replies:
    2
    Views:
    392
  3. NaeiKinDus
    Replies:
    1
    Views:
    578
    Jack Klein
    Apr 14, 2007
  4. NaeiKinDus
    Replies:
    3
    Views:
    612
    James Kanze
    Apr 15, 2007
  5. Replies:
    12
    Views:
    977
Loading...

Share This Page