|
-
Mar 11th, 2008, 11:19 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Threading in Java to display numbers.
Hi all,
I want to print some numbers, say starting from 1 and increment by 1, in a specific interval, say each one second.
I want to use Java thread to do this. Can you guys give me a clue.
I've try this, but I can't execute on main(). When I call the init() method, thread stopped.
Code:
public class Counter extends Applet implements Runnable {
Thread t;
int Count;
@Override
public void init()
{
Count=0;
t=new Thread(this);
t.start();
}
@Override
public boolean mouseDown(Event e,int x, int y)
{
t.stop();
return true;
}
public void run()
{
while(true)
{
Count++;
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}
}
@Override
public void paint(Graphics g)
{
//g.drawString(Integer.toString(Count),10,10);
System.out.println("Count= "+Count);
}
@Override
public void stop()
{
t.stop();
}
}
public class Main {
public static void main(String[] args) {
Counter ss = new Counter();
ss.run();
}
}
Thanks.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|