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]

[1725 byte] By [benjaminCcca] at [2007-11-26 21:56:05]
# 1
Still no "?"
DrLaszloJamfa at 2007-7-10 3:52:23 > top of Java-index,Java Essentials,New To Java...
# 2
> 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?
kikemellya at 2007-7-10 3:52:23 > top of Java-index,Java Essentials,New To Java...
# 3
Do the both part together
benjaminCcca at 2007-7-10 3:52:23 > top of Java-index,Java Essentials,New To Java...
# 4

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

}

kikemellya at 2007-7-10 3:52:23 > top of Java-index,Java Essentials,New To Java...
# 5
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.
TuringPesta at 2007-7-10 3:52:23 > top of Java-index,Java Essentials,New To Java...
# 6

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;

}

TuringPesta at 2007-7-10 3:52:23 > top of Java-index,Java Essentials,New To Java...
# 7
OKOK ....i know that now , thank you for your help ~~
benjaminCcca at 2007-7-10 3:52:23 > top of Java-index,Java Essentials,New To Java...
# 8

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();

}

}

kikemellya at 2007-7-10 3:52:23 > top of Java-index,Java Essentials,New To Java...
# 9

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.

TuringPesta at 2007-7-10 3:52:23 > top of Java-index,Java Essentials,New To Java...
# 10
> 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.
DrClapa at 2007-7-10 3:52:23 > top of Java-index,Java Essentials,New To Java...
# 11

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

kikemellya at 2007-7-10 3:52:23 > top of Java-index,Java Essentials,New To Java...