Results 1 to 2 of 2

Thread: JAVA - How to make Threads

  1. #1

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    JAVA - How to make Threads

    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.

    Code:
    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();
                }
            }
        }
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  2. #2
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Re: JAVA - How to make Threads

    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.

    Code:
    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!

    Also, you should **NEVER** stop() a thread; the the method is deprecated for very good reasons!
    Last edited by CaptainPinko; Feb 3rd, 2005 at 01:39 AM.
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width