Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Multi Threading
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Bo0sT




PostPosted: Thu Mar 27, 2008 5:23 pm   Post subject: Multi Threading

does any1 know how to run a class on a seperate thread than the rest of the program? what i am doing is using JPanels, each is it's own method and each overrides a paint method. i am doing this so that i can have 2 screens for my game. one for the actual game and another for a timer. i put Thread.sleep in one of the methods within the subclass the controls and displays my timer JPanel. but the Thread.sleep isnt only delaying my timer, but also the actual game freezes for 1 second every second, just as the timer is. i think this is because it is all running on the same thread, how can i put my timer on a seperate thread. iv searched the web and i now know how to make a new thread, but i dont know how to run my timer on it, if any1 could help, that would be great
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Thu Mar 27, 2008 5:30 pm   Post subject: RE:Multi Threading

You don't have to use Thread.sleep to keep track of the timer. It is inaccurate, because it takes some random amount of time (even if it is only milliseconds) to increment a timer variable.

You can get the time your program has been running in millliseconds with System.currentTimeMillis(). That would be a better option to keep track of a timer.
Vermette




PostPosted: Thu Mar 27, 2008 5:33 pm   Post subject: Re: Multi Threading

If you want to instantiate a class in its own thread it needs to be implemented with the Runnable interface

Java:

class Foo implements Runnable{
   public foo(){

   }
   /*class methods
...
...
...
*/

//classes with the Runnable interface need to implement run
   public void run(){
      //class threading code goes here
   }
}
Bo0sT




PostPosted: Thu Mar 27, 2008 5:42 pm   Post subject: Re: Multi Threading

so i dont need to make an instance of the Thread object at all?
how would i set the properties of the thread?

and im using Thread.sleep like this, in the form of a method with a paramater

code:

import java.util.*;
public class delay
{
     public static void main (String[]args)
     {
          delay(1000); //delays for 1 second
     }
     void delay(int t)
     {
          try
          {
                Thread.sleep(t)
          }
          catch(Exception e){}
     }
}
Bo0sT




PostPosted: Thu Mar 27, 2008 5:55 pm   Post subject: Re: Multi Threading

ok i just tried implementing the runnable interface and using the run method, but my problem now is that i cant paint inside of my run method or call my paint method from inside of my run method, means i cant draw the timer on the screen D= im drawing it using the drawString method
Bo0sT




PostPosted: Thu Mar 27, 2008 8:47 pm   Post subject: Re: Multi Threading

ok i looked around and i think i found something, but iv got a new problem again, my run and start methods just simply are not starting, iv implemented runnable, iv call start(); method, i dunno wut it is this time
Bo0sT




PostPosted: Thu Mar 27, 2008 9:01 pm   Post subject: Re: Multi Threading

can somebody please tell me why run and start arent working...

code:

class Timer extends JPanel implements Runnable
{
    static int counter = 10;
        static Thread th;

    public void paint (Graphics g)
    {
        clear (g);
        g.drawString ("" + counter, 10, 10);
    }
                public void run ()
        {
                System.out.println("Run works");
                counter--;
                repaint();
                delay(1000);
        }
        public void start ()
        {
                System.out.println("Start works");
                th = new Thread(this);
                th.start();
        }

    void clear (Graphics g)
    {
                g.setColor (Color.white);
                g.fillRect (0, 0, 400, 400);
                g.setColor (Color.black);
    }


    void delay (int t)
    {
                try
                {
                    th.sleep (t);
                }
                catch (Exception e){}
    }
}
HellblazerX




PostPosted: Thu Mar 27, 2008 9:19 pm   Post subject: RE:Multi Threading

I noticed your Thread variable is static. Does that mean you trying to run your thread without instantiating your class, because you don't have a constructor. Is that even possible?
Sponsor
Sponsor
Sponsor
sponsor
r691175002




PostPosted: Thu Mar 27, 2008 9:21 pm   Post subject: Re: Multi Threading

I think you are approaching this the wrong way. Threads should not be necessary for what you are trying to accomplish.
If I understand you properly, you are trying to have to different panels , one showing a game, the other which redraws itself once a second.
You are telling the game to stop execution for one second inside the timer panel which is, as expected, stopping execution.

There is absolutely no need for a Thread.sleep(1000); inside the timer panel.
You have a few options, you can only draw the timer once per second (And let it count upwards itself) using something like:
code:
long n = 1000 + System.currentTimeMillis();
while (true) {
    renderGame();
    if (System.currentTimeMillis() > n) {
        renderTimer();
        n = 1000 + System.currentTimeMillis();
    }
}

Which will only call renderTimer(); once per second. Or something like:
code:
public void renderTimer() {
    drawString ("The time is: " + (System.currentTimeMillis() - offset )/1000 + " Seconds...");
}

Which will draw every frame, but represent the correct value.

Or you could scrap your JPanel and custom paint methods and go with a JLabel that will do the drawing for you.

Threads are completely unnecessary and a bad idea considering that you are new to them. I have a strong suspicion that the threaded solution you are working on right now has at least one race condition.
As well, calling paint(); from outside of the swing thread is heavilly discouraged because of threading issues. I suggest picking up a book, I believe that Killer Java Game Programming has a somewhat complete copy on google books, and a complete earlier edition for free online.


There are more ways to create a timer than I can list, and of them all, creating a dedicated thread for it is one of the worst. Note that Thread.sleep() is not completely accurate, and the inaccuracies will build up over time even if you do accomplish a threaded solution, especially if there are other high priority processes working.
Bo0sT




PostPosted: Fri Mar 28, 2008 12:12 am   Post subject: Re: Multi Threading

thank you vary much, i will post a reply when i get this working
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: