Tutorials

Philfrei, that seemed to work, thanks. Does that mean the compiler was using some default run method instead of the one I wrote for it?

Haha wow I can’t believe I didn’t notice that. Yeah you need to give Thread the actual Runnable who’s run() method should be called, else it uses an empty run() method :stuck_out_tongue:

To many links :)…

Click em all!!

Try this code:

import java.applet.*;
import java.awt.*;

public class SampleThread extends Applet implements Runnable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L; //This is a default ID
	Thread th;
	int i;
        //Initialize vars and such...
	public void init()
	{
		th = new Thread();
		i = 0;
	}

       //Start the Thread...
	public void start() {
		th = new Thread(this);
		th.start();
	}

	public void stop() { //Stopping a thread is important, and destroy() is deprecated so we use this instead
		th = null;
	}

	@Override
	public void run() {
		Thread thisThread = Thread.currentThread();

		while (th == thisThread) {//just makes sense there, if th is null (say, if we wanted it to stop by using stop(), 
                //or if it was null because you closed the application straight up) then it wont equal thisThread 
                 //and it will stop without giving you an error
			i++;
			repaint();

			try{
				Thread.sleep(1000);
			} catch (InterruptedException e) {
                             break; //why didn't you have this there in the first place? 
                        }
		}
	}

	public void paint(Graphics g)
	{
		g.drawString("i = "+i, 10, 20);
	}
}

If that doesn’t work it’s gotta be something wrong with the HTML or something else…