Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Stack overflow and my dang thread  (Read 1042 times)
0 Members and 1 Guest are viewing this topic.
Offline straydoginc

Junior Member





« Posted 2005-11-02 21:26:26 »

Okay.. Im using the following class (minus the constants string value with the full text) to attempt to create text that displays on the screen in a time delayed way per character(video game wonderment) and in trying to pause it I attempted to create a thread that then caused my top 4 lines to keep creating the class and a stack overflow emerged.

Question.. given my code below. What do i need to do to make the thread without stack overflowing and pause the text and end the thread.

After much Googling i am very confused on this subject.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
public class TextDelay extends Applet implements Runnable{

   private String             screenMessage = ""; //stars empty
  private int              messagePos = 0;
   
    // Create the object with the run() method
   Runnable runnable = new TextDelay();
   
    // Create the thread supplying it with the runnable object
   Thread thread = new Thread(runnable);
   
    private boolean allDone = false;
   
    public void run() {
//        Start the thread
      thread.start();
       
     }

   
   public void paint ( Graphics g )
    {
     

       
     
//      update the message
     while (messagePos != ConstantsT.message.length()){
         
         try {
              long numMillisecondsToSleep = 1000; // 1 seconds
             Thread.sleep(numMillisecondsToSleep);
          } catch (InterruptedException e) {
          }

            screenMessage = screenMessage +  ConstantsT.message.charAt(messagePos);
            messagePos++;
         if (messagePos == ConstantsT.message.length()){
            //thread.allDone = true ;
           break;
         }
     
      }
       //End of message.
 
            g.setColor   ( Color.blue );
            g.drawString ( screenMessage, 10 , 10 );
    }


   
}
Offline cborders

Junior Member





« Reply #1 - Posted 2005-11-02 23:12:32 »

 Undecided I am not exactly sure what's going on in your class!  You've implemented runnable and your creating new runnables ...  Huh
Here's a way that you could do it, then you would just have to allocate a long variable per block of text and the rest could be taken care of in your game loop!  So the run() method in this example represents the game logic section of your code and the paint() would be the actual rendering part of your code.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class TypeWriter extends JFrame implements Runnable
{
   private final String message = "Typewriter-Style Baby!";
   private StringBuffer currMessage = new StringBuffer();
   private int currLetter = 0;
   private Thread thread;
   private long time = 0;
   
   public static void main(String[] args)
   {
      new TypeWriter();
   }

   public TypeWriter()
   {
      addWindowListener(new WindowAdapter(){
         public void windowClosing(WindowEvent we)
         {
            stop();
            System.exit(0);
         }
      });
      setSize(400, 400);
      setVisible(true);
      start();
   }
   
   public void run()
    {
        Thread me = Thread.currentThread();
        while (thread == me)
        {
           if(time == 0 || System.currentTimeMillis() >= time)
           {
              currMessage.append(message.charAt(currLetter));
              currLetter++;
              repaint();
              if(currLetter >= message.length()) stop();
              time = System.currentTimeMillis() + 500;
           }
           else
           {
              try
              {
                 Thread.sleep(100);
              }
              catch(InterruptedException ie)
              {}
           }
        }
        thread = null;
    }
   
   public void paint(Graphics g)
   {
      g.drawString(currMessage.toString(), 10, 40);
   }
   
    public void start()
    {
       thread = new Thread(this);
       thread.setPriority(Thread.MIN_PRIORITY);
       thread.start();      
    }

    public void stop()
    {
       thread = null;
    }
}


I hope this helps!   Grin
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #2 - Posted 2005-11-03 00:51:41 »

I know exactly what is going on. You're making an infinite loop. How? Read on.

When you call .start() in a Thread, the run() method happens. So if you wanted to make a thread to print out foo, fifty times, you would do this:
1  
2  
3  
4  
5  
6  
7  
8  
private class FooPrint implements Runnable
{
    public void run()
    {
        for (int i = 0; i < 50; i++)
            System.out.println("foo");
    }
}


and you would call it like this:
1  
2  
3  
4  
5  
//Creates the thread, but nothing happens yet.
Thread thread = new Thread(new FooPrint());
//Now you actually will cause the printing by telling
//the thread to start, ie calling its run() method
thread.start();


The way you are doing it creates an infinite loop, because your run method calls the start() method which calls the run() method which calls the start() method etc. etc. etc.

A StackOverflow is generally caused by infinite loop, which is just the case right here.

Instead of saying thread.start() in your run() method, remember that start() always happens before run(), and your run method contols everything... so just change that line to whatever you want to happen in your thread.

Also, your code definitely needs some other work too. You generally would not make a new thread to pause an animation, for example. Rather, you would use a new thread to do something like the opposite – perhaps move an animation at the same time something else is moving it without modifying the main game loop. To pause an animation just pause your game loop, or don't call the paint() method for that amount of pause time.

Where are you calling paint? The whole structure of your class could use some work. Instead of making the entire class the thread, make a private class that only has the run method in it. This makes everything less confusing and I guarantee you will have less problems.

Hope that all made sense. Smiley

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline erikd

JGO Ninja


Medals: 15
Projects: 4
Exp: 14 years


Maximumisness


« Reply #3 - Posted 2005-11-03 11:09:08 »

What also catched my attention is that you instantiate an object of the same type while instantiating in:

Runnable runnable = new TextDelay();

So when you instantiate a TextDelay, you instantiate another TextDelay which instantiates another TextDelay, etc etc

Offline straydoginc

Junior Member





« Reply #4 - Posted 2005-11-07 19:27:54 »

Yup after reading this and using my new found debug feature I was creating the class over and over again. This whole program needs to run in an applet so Im not sure on the JFrame code but can see what I can do. This example should get me to where I need to be. Basically Ive set it up so that a string is typed out to the window at your cpus refresh rate. Im trying to slow that down so it does more of a game type feel. Tried a this.wait(1000) and that gave me a lovely other error. So threading is the way to go.

So Ill try to see what I can do about the threading. Any suggestions would be alsome and thanx for the great examples Smiley
Offline straydoginc

Junior Member





« Reply #5 - Posted 2005-11-07 20:37:52 »

Thanx Cborders.. I used that example and got it to do what I wanted it to do !!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #6 - Posted 2005-11-08 01:00:00 »

If you use JApplet instead of JFrame it's almost exactly the same thing.

See my work:
OTC Software
Offline cborders

Junior Member





« Reply #7 - Posted 2005-11-08 20:05:31 »

 Grin Sweet!  Glad I could help!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (30 views)
2014-04-15 18:08:23

BurntPizza (27 views)
2014-04-15 03:46:01

UprightPath (42 views)
2014-04-14 17:39:50

UprightPath (24 views)
2014-04-14 17:35:47

Porlus (41 views)
2014-04-14 15:48:38

tom_mai78101 (63 views)
2014-04-10 04:04:31

BurntPizza (123 views)
2014-04-08 23:06:04

tom_mai78101 (222 views)
2014-04-05 13:34:39

trollwarrior1 (189 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!