[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.
Re: Threading in Java to display numbers.
Try this
Code:
import java.util.logging.Level;
import java.util.logging.Logger;
public class Util {
public static void main(String[] args) throws Exception {
Thread th = new Thread(new Runnable() {
public void run() {
int i = 0;
while (i <= 10) {
try {
System.out.println(i++);
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
th.start();
}
}
Re: Threading in Java to display numbers.
And you can't use the main method to display an applet, you must have a containing browser. so put it in an HTML page
Re: Threading in Java to display numbers.
Oh dear, it's a terrible mistake I have make. Yes you are right, and mess-up on my IDE I think.
Re: Threading in Java to display numbers.
Quote:
Originally Posted by ComputerJy
Try this
Code:
import java.util.logging.Level;
import java.util.logging.Logger;
public class Util {
public static void main(String[] args) throws Exception {
Thread th = new Thread(new Runnable() {
public void run() {
int i = 0;
while (i <= 10) {
try {
System.out.println(i++);
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
th.start();
}
}
Thanks pal.
This code working as I expected. I've implement it on a button click-event and it works fine. But mess up one thing on my GUI.
Until the thread execution completed, all the controls(I mean buttons, check boxes, etc) are freeze. That mean I can't click any button, check-uncheck a check box and so on. Not the whole frame, I can move frame only.
What can be wrong.
After execution of the thread completed, I can use all controls.
Re: Threading in Java to display numbers.
Thread.sleep(1000); causes the main thread to suspend all execution for 1000 milliseconds
Re: Threading in Java to display numbers.
Yes, initially I guess that do something like this in that thread.
Code:
int x = 1;
Thread th = new Thread(new Runnable() {
public void run(){
int counter = 0;
while(counter <= 10000){
System.out.println(counter++);
}
}
});
But it not work. As I said early, until print the number 10000 to console, I can't use any controls on the GUI.
Any idea pal...
Re: Threading in Java to display numbers.
Actually something is wrong with the way you're organizing your code. Because a simple test
Code:
import java.util.logging.Level;
import java.util.logging.Logger;
public class Util {
public static void main(String[] args) throws Exception {
Thread th = new Thread(new Runnable() {
public void run() {
int i = 0;
while (i <= 10) {
try {
System.out.println(i++);
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
th.start();
Thread.sleep(500);
for (char c = 'a'; c <= 'z'; c++) {
System.out.println(c);
}
}
private Util() {
}
}
proves that if you call Thread.sleep the current thread only will be suspended not the whole system.
Re: Threading in Java to display numbers.
Ok, I try what you say. But I got an error on following lines. Actually I cant use the thread th again.
Error: package th does not exit
Error: illegal start of type
I used the thread in the button click event as follows.
Code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Util appProcess = new Util();
appProcess.th.run();
}
But still the same thing is happened. :o All those things going on two separate classes right now, as you can see I have create instance for the Util class where I have implement my thread.
Re: Threading in Java to display numbers.
Ok pal, I have observed one thing here. I simply create a thread in the same class where I create GUI. Here is my thread.
Code:
Thread testTH = new Thread(new Runnable() {
public void run(){
int counter = 0;
while(counter <= 10){
try {
System.out.println(counter++);
Thread.sleep(1000);
}
catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
});
Then in the click-event I simply call the start() of the thread.
It works fine. So what is wrong with my code.
Re: Threading in Java to display numbers.
I don't know
I haven't seen your code!!
Re: Threading in Java to display numbers.