T
Tyler Kellen
Why does this code hang for the specified 5 seconds (in the refresh
thread)before displaying anything? I have the sleep running in a
seperate thread but it causes everything to wait for it. The desired
result I am trying to accomplish here is to add another line of text
to the screen every 5 seconds. I'm not certain how to go about this
though. Any help understanding where I am going wrong here would be
greatly appreciated.
//-->Start Code
import java.applet.Applet;
import java.awt.Graphics;
public class test extends Applet
{
StringBuffer buffer;
refresh r;
public void init()
{
buffer = new StringBuffer();
addItem("initializing... ");
r = new refresh();
}
public void start()
{
addItem("starting... ");
r.start();
}
void addItem(String newWord)
{
buffer.append(newWord);
repaint();
}
public void paint(Graphics g)
{
g.drawString(buffer.toString(), 5, 15);
}
class refresh extends Thread
{
public void start()
{
try { refresh.sleep(5000); } catch(Exception e) { }
addItem("refreshed");
}
}
}
//--> End Code
If I put the refresh start() contents in a while(true) { } loop it
never displays anything. How do I have this loop running but allow
the main thread to continue?
Thanks much,
Tyler Kellen
thread)before displaying anything? I have the sleep running in a
seperate thread but it causes everything to wait for it. The desired
result I am trying to accomplish here is to add another line of text
to the screen every 5 seconds. I'm not certain how to go about this
though. Any help understanding where I am going wrong here would be
greatly appreciated.
//-->Start Code
import java.applet.Applet;
import java.awt.Graphics;
public class test extends Applet
{
StringBuffer buffer;
refresh r;
public void init()
{
buffer = new StringBuffer();
addItem("initializing... ");
r = new refresh();
}
public void start()
{
addItem("starting... ");
r.start();
}
void addItem(String newWord)
{
buffer.append(newWord);
repaint();
}
public void paint(Graphics g)
{
g.drawString(buffer.toString(), 5, 15);
}
class refresh extends Thread
{
public void start()
{
try { refresh.sleep(5000); } catch(Exception e) { }
addItem("refreshed");
}
}
}
//--> End Code
If I put the refresh start() contents in a while(true) { } loop it
never displays anything. How do I have this loop running but allow
the main thread to continue?
Thanks much,
Tyler Kellen