Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to break a thread in another class

Reply
Thread Tools

how to break a thread in another class

 
 
aleel
Guest
Posts: n/a
 
      04-25-2004
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 ?
 
Reply With Quote
 
 
 
 
chris
Guest
Posts: n/a
 
      04-25-2004
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 http://www.velocityreviews.com/forums/(E-Mail Removed)
/k/ Embedded Java Solutions

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
`if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ? lovecreatesbea...@gmail.com C Programming 12 04-14-2008 07:59 AM
problem mutex-thread "Unlocking mutex owned by another thread ???" NaeiKinDus C++ 1 04-14-2007 07:40 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
How to avoid a thread stealing the turn of another thread? Nafai Java 2 04-24-2005 03:49 PM
Passing result from one thread to another thread at the end of execution sayoyo Java 3 02-16-2004 03:39 PM



Advertisments