Results 1 to 13 of 13
  1. #1
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Trouble wih threads

    Hi,

    I have one single thread that i wish to start but whatever i try, i can't make it work. I start the thread in the main Class' constructor and call the constructor from main().

    Here's a very cut down version:

    Java Code:
    public class TwoDWorld extends JPanel implements KeyListener, Runnable{
    
        Thread t;
        long startingTime = 0;
        long passedTime = 0;
    
        public TwoDWorld() {
            t = new Thread();   
            t.start();
            }
    
    	public static void main(String args[]) {
    		new TwoDWorld();
    	}
    
    	public void run() {
    		long startingTime = System.currentTimeMillis();
    		long totalTime = startingTime;
    		System.out.println("hey");
    		while (true) {
    			try {
    				passedTime = System.currentTimeMillis() - totalTime;
    				totalTime += passedTime;
    				
    				update();
    				draw();				
    				render();
    				t.sleep(50);
    				
    			} catch (Exception e) {}
    		}
    		
    	}
    My thread is not starting for some reason because "hey" is not shown in the command line.
    Any thoughts?

    Thanks, James.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Trouble wih threads

    What connects the Thread object you "start" with any code to be executed?

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Trouble wih threads

    You have to pass your Runnable to the Thread object so that object knows what Runnable interface to run, otherwise it will run its default.

  4. #4
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Trouble wih threads

    But doesn't implementing runnable start the run(); method when start(); is called?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Trouble wih threads

    Yes, the start method causes the Thread class's run method to be executed.
    Where do you tell the Thread class about your run() method?

    Read the API doc for the Thread class's run method.

  6. #6
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Trouble wih threads

    I now start the thread like this:
    Java Code:
    Thread t;
    
    public TwoDWorld() {        
            TwoDWorld tdw = new TwoDWorld();
            t = new Thread(tdw);
            t.start();
    }
    The thread starts fine but something is causing windows to rapidly open.
    Why? Im lost

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Trouble wih threads

    EDIT:
    Sorry I am answering questions on another thread about this same topic and have confused my answers.

    something is causing windows to rapidly open.
    Can you explain what happens?
    Last edited by Norm; 09-30-2011 at 04:31 AM.

  8. #8
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Trouble wih threads

    Throws:
    IllegalThreadStateException - if the thread was already started.

    It just runs until i get memory errors.
    Last edited by jammas615; 09-30-2011 at 04:34 AM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Trouble wih threads

    See my last post. I was on another similiar topic and got them confused.

    something is causing windows to rapidly open.
    Can you explain what happens?

  10. #10
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Trouble wih threads

    When i run the program, a black window opens, and across the windows taskbar, a whole line of the same process opens and just keeps opening rapidly. I then start to get errors like this:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    	at java.awt.image.DataBufferInt.<init>(Unknown Source)
    	at java.awt.image.Raster.createPackedRaster(Unknown Source)
    	at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
    	at java.awt.GraphicsConfiguration.createCompatibleImage(Unknown Source)
    	at java.awt.GraphicsConfiguration.createCompatibleImage(Unknown Source)
    	at sun.awt.image.SunVolatileImage.getBackupImage(Unknown Source)
    	at sun.awt.image.VolatileSurfaceManager.getBackupSurface(Unknown Source)
    	at sun.awt.image.VolatileSurfaceManager.initialize(Unknown Source)
    	at sun.awt.image.SunVolatileImage.<init>(Unknown Source)
    	at sun.awt.image.SunVolatileImage.<init>(Unknown Source)
    	at sun.awt.image.SunVolatileImage.<init>(Unknown Source)
    	at sun.awt.image.SunVolatileImage.<init>(Unknown Source)
    	at sun.awt.windows.WComponentPeer.createVolatileImage(Unknown Source)
    	at java.awt.Component.createVolatileImage(Unknown Source)
    	at java.awt.Component$BltBufferStrategy.createBackBuffers(Unknown Source)
    	at java.awt.Component$BltBufferStrategy.<init>(Unknown Source)
    	at java.awt.Component$BltSubRegionBufferStrategy.<init>(Unknown Source)
    	at java.awt.Component.createBufferStrategy(Unknown Source)
    	at java.awt.Window.createBufferStrategy(Unknown Source)
    	at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(Unknown Source)
    	at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(Unknown Source)
    	at javax.swing.BufferStrategyPaintManager$BufferInfo.getBufferStrategy(Unknown Source)
    	at javax.swing.BufferStrategyPaintManager.prepare(Unknown Source)
    	at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    	at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    	at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    	at java.awt.Container.paint(Unknown Source)
    	at java.awt.Window.paint(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Trouble wih threads

    I don't see any references to your program's classes in the stack trace.
    Try adding lots of printlns to your code to show the execution flow to find the location where the error is occuring.

  12. #12
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Trouble wih threads

    This line:
    Java Code:
    TwoDWorld tdw = new TwoDWorld();
    Was causing it.
    I changed it to this:

    Java Code:
    t = new Thread(this);
    t.start();

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Trouble wih threads

    You had a recursive loop: the constructor was calling the constructor over and over.
    Did you leave off some of the stack trace that showed that?

Similar Threads

  1. How to use two threads
    By rajan701v in forum Threads and Synchronization
    Replies: 2
    Last Post: 07-22-2011, 06:47 PM
  2. Threads
    By GoLuM83 in forum Advanced Java
    Replies: 7
    Last Post: 03-31-2010, 09:52 PM
  3. Threads!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-04-2008, 05:11 PM
  4. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 11:25 AM
  5. Threads
    By one198 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 07:15 PM

Posting Permissions

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