Extending the Thread class
i would like to do that
1) One thread displays "ABC" every 2 second;
2) The other thread displays DEF every 5 seconds;
i need to create the threads by extending the Thread class ...
thank you for your help ,
publicclass Thread1extends Thread{
public Thread1(String s ){
super (s);
}
publicvoid run(){
for (int i=0; i<5; i++ ){
System.out.println(getName());
try{
sleep ((long) 5000);
}catch (InterruptedException e ){
/* do nothing */
}
}
}
publicstaticvoid main (String args[]){
new Thread1 ("ABC").start();
new Thread1 ("DEF").start();
}
}
[code]
> i would like to do that > 1) One thread displays "ABC" every 2 second;> 2) The other thread displays DEF every 5 seconds;Kewl. So which part are you stuck on?
Do the both part together
Well think about it, to do this you will need a way to identify each thread. How about using there name? Then you will need to say something like:
if(getName().equals("ABC"))
{
//code here to sleep for 2 seconds
}
else
{
//code here to sleep for 5 seconds
}
Sigh...Your code requirements arent adding Threading functionality to the thread class. Rather, your requirement is to write somethingthat RUNS in a Thread.The point being... you should (as almost always) implement Runnableand NOT extend Thread.
public class RepeatThread implements Runnable{
public static void main (String args[]) {
Thread t1 = new Thread(new RepeatThread(1000), "ABC");
t1.start();
Thread t2 = new Thread(new RepeatThread(3000), "DEF");
t2.start();
}
public RepeatThread(int delay){
this.delay = delay;
}
public void run(){
try{
while(true){
System.out.println(Thread.currentThread().getName());
Thread.sleep(delay);
}
} catch(Exception e){}
}
int delay;
}
OKOK ....i know that now , thank you for your help ~~
I think he has been told to use the Thread class by the sounds of it.
public class Thread1 extends Thread {
public Thread1(String s ) {
super (s);
}
public void run() {
for ( int i=0; i<5; i++ ) {
System.out.println(getName());
try {
sleep (getName().equals("ABC")? 5000 : 2000); //If you don't understand this then Google for "Java ternary operator"
} catch (InterruptedException e ) {
/* do nothing */
}
}
}
public static void main (String args[]) {
new Thread1 ("ABC").start();
new Thread1 ("DEF").start();
}
}
why
sleep (getName().equals("ABC")? 5000 : 2000);
wouldnt it make more sense to send a delay value to the constructor
public Thread1(String s, int delay ) {
I mean, if youre going to do the wrong thing and extend thread then
you might as well add a useful constructor, lol.
> I mean, if youre going to do the wrong thing and> extend thread then> you might as well add a useful constructor, lol.Absolutely. And the right thing here, if this were a real task, would be to use a pair of java.util.Timer objects.
> why
> sleep (getName().equals("ABC")? 5000 : 2000);
> wouldnt it make more sense to send a delay value to
> the constructor
> public Thread1(String s, int delay)
> I mean, if youre going to do the wrong thing and
> extend thread then
> you might as well add a useful constructor, lol.
Well if I was writing the code I would extend runnable unless I had a good reason to extend Thread but when someone says "I need to extend the Thread class" then thats what I base my reply around. It sounds like a homework assignment anyways and he has probably been specifically told this is how he has to approach this task.