Results 1 to 13 of 13
Thread: Trouble wih threads
- 09-30-2011, 01:40 AM #1
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
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:
My thread is not starting for some reason because "hey" is not shown in the command line.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) {} } }
Any thoughts?
Thanks, James.
- 09-30-2011, 02:14 AM #2
Re: Trouble wih threads
What connects the Thread object you "start" with any code to be executed?
- 09-30-2011, 02:15 AM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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.
- 09-30-2011, 02:17 AM #4
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: Trouble wih threads
But doesn't implementing runnable start the run(); method when start(); is called?
- 09-30-2011, 02:23 AM #5
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.
- 09-30-2011, 03:24 AM #6
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: Trouble wih threads
I now start the thread like this:
The thread starts fine but something is causing windows to rapidly open.Java Code:Thread t; public TwoDWorld() { TwoDWorld tdw = new TwoDWorld(); t = new Thread(tdw); t.start(); }
Why? Im lost
- 09-30-2011, 03:28 AM #7
Re: Trouble wih threads
EDIT:
Sorry I am answering questions on another thread about this same topic and have confused my answers.
Can you explain what happens?something is causing windows to rapidly open.Last edited by Norm; 09-30-2011 at 03:31 AM.
- 09-30-2011, 03:29 AM #8
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
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 03:34 AM.
- 09-30-2011, 03:35 AM #9
Re: Trouble wih threads
See my last post. I was on another similiar topic and got them confused.
Can you explain what happens?something is causing windows to rapidly open.
- 09-30-2011, 03:38 AM #10
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
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)
- 09-30-2011, 03:42 AM #11
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.
- 09-30-2011, 03:51 AM #12
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: Trouble wih threads
This line:
Was causing it.Java Code:TwoDWorld tdw = new TwoDWorld();
I changed it to this:
Java Code:t = new Thread(this); t.start();
- 09-30-2011, 03:55 AM #13
Similar Threads
-
How to use two threads
By rajan701v in forum Threads and SynchronizationReplies: 2Last Post: 07-22-2011, 05:47 PM -
Threads
By GoLuM83 in forum Advanced JavaReplies: 7Last Post: 03-31-2010, 08:52 PM -
Threads!
By rameshraj in forum Advanced JavaReplies: 1Last Post: 05-04-2008, 04:11 PM -
Using threads
By Java Tip in forum Java TipReplies: 0Last Post: 12-11-2007, 10:25 AM -
Threads
By one198 in forum Threads and SynchronizationReplies: 1Last Post: 11-20-2007, 06:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks