# VBForums CodeBank > CodeBank - Java / J# >  JAVA - How to make Threads

## BramVandenbon

I just wanted to post an example on using Threads in Java.

most important things are ...
(1) making a class that extends the Thread class and implements the Runnable interface.
(2) add the run method to that class and put your functionallity in it
(3) make an instance of your class and set priority of that instance on low.
(4) start and stop the instance.

Enjoy.



```
import java.awt.*;
import javax.swing.*;

public class ExampleWindow extends JFrame{
    int Frames_Per_Second = 45;
    autoRefresh engine = new autoRefresh(Frames_Per_Second);

    public static void main(String args[]) {
        (new ExampleWindow()).show();
    }

    public ExampleWindow() {
        speelveld.setPreferredSize(new Dimension(500,500));
        getContentPane().setLayout(new java.awt.FlowLayout());
        pack();
        
        //constant refreshing
        engine.setPriority(Thread.MIN_PRIORITY);
        engine.start();
        
        //closing the window
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                engine.stop();
                System.exit(0);
            }
        });
    }
    
    class autoRefresh extends Thread implements Runnable{
        int ms_stop = 0;

        public autoRefresh(int fps){
            ms_stop = 1000 / fps;
        }

        public void run(){
            while (true){
                /*
                .... Add functionality here ...
                I use it for repainting my supermario game
                */
                waiting(ms_stop);
            }
        }
        
        private void waiting(long ms){
            long thetime = System.currentTimeMillis();
            boolean blnrepeat;
            while (blnrepeat){
                blnrepeat = (thetime + ms) > System.currentTimeMillis();
            }
        }
    }
```

----------


## CaptainPinko

Well, technically you need to only extend java.lang.Thread*or* implement java.lang.Runnable. Unless you are defining a special type of thread (a is-a relation) you should be implementing runnable instead. Also your code could be simpler.



```
public class ThreadDemo (String [] args){
	static final int MAX_THREAD = 5;
	CounterThread [] threads = new CounterThread [MAX_THREAD];
	for (int i=0; i < MAX_THREAD; i++){
		threads [i] = new CounterThread ();
		threads [i].run ();
	}

}
class CounterThread implements Runnable {
     static int threadTotal = 0;
     int threadNum;
     public CounterThread () {threadNum=threadTotal++;}
     public void run (){
          for (int = 0; i < 16; i++)
               {System.out.println("Thread " + threadNum + " says " + i);}
     }
}
```

Also this program shows that thread scheduling is not deterministic from the programmers point of view, this should be run a few times to see what I mean. The code as a whole should run fine, but there might be some minor syntax errors... those are left as an excercise to the reader!   :big yellow:  

Also, you should ***NEVER*** stop() a thread; the the method is deprecated for very good reasons!

----------

