Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (762)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (847)
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  
  WindowListener and Threads  (Read 1609 times)
0 Members and 1 Guest are viewing this topic.
Offline Gregg Bolinger

Senior Newbie





« Posted 2005-04-11 18:54:52 »

I created a class that implements runnable and in my run() method is where I do my animation loop.  I start the thread from my class constructor after everything is initialized.  2 questions....

Is this ok to do or will I run into thread problems later and how can I add a window listener so that I can be notified when the Xith container is closing so I can stop the thread first?

Thanks.

Gregg
Offline Matthias

Senior Devvie


Medals: 5
Projects: 1


TWL - Themable Widget Library


« Reply #1 - Posted 2005-04-11 19:16:12 »

I didn't got any problems when I start the thread (and the rendering) from the constructor.

For the shutdown I do the following:
...
windowClosing(WindowEvent ev) {
 try {
   myThread.interrupt();
   myThread.join();
 } catch (InterruptedException ex) {
 }
 dispose();
}
void windowClosed(WindowEvent ev) {
 System.exit(0);
}
....
void run() {
 try {
    while(!Thread.interrupted()) {
       ...
    }
 } catch (InterruptedException ex) {
 }
}
...


This will reult in a smooth shutdown without any GL exceptions because the canvas is only destroyed (by dispose()) after the thread has stopped and withit all GL commands.

Ciao Matthias Mann
Offline Gregg Bolinger

Senior Newbie





« Reply #2 - Posted 2005-04-11 19:19:58 »

Thanks. To what component to I call addWindowListener()?

Gregg
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline hawkwind

Junior Devvie




Java games rock!


« Reply #3 - Posted 2005-04-12 00:48:07 »

I have concerns over this approach in general, having been bit by pc's of various cpu speeds.  If you doing any scene manip at all in a separate thread you stand a chance of updating the scene while its being rendered.

The best approach is to create a system of "scene transactions" in a queue and process some percentage of the queue each render cycle (percentage of transactions is a funtion of any required FPS, you may not be able to do everything in every cycle)

Unless your animstion thread is tightly synchronized with the render loop...trouble looms in the future.  If you are using some form of sync then your frame rate may suffer.

For example suppose you are firing sheels at a bunker you might have a queue containing

0 - translate Shell  A  to x,y,z
1 - rotate turret  Y  5 degrees
2 - update explosion particles 1 time
3 - increase output of light A 5% (for explosion affect)

in your render loop, pop the queue N time and perform until out of allocated animation time.

repeat...


jest a thought....my first version of my game had several threads, worked like a charm until run on a fast machine  KABLAMM!!!

a further advantage is you can adjust translations etc based on the actual frame rate (when fast position change is in smaller incrementals, when slow move things farther each cycle.  6 steps moving 2 feet  or 3 steps moving 4 feet...they both move you 12 spaces...

Offline William Denniss

JGO Coder


Projects: 2


Fire at will


« Reply #4 - Posted 2005-04-12 20:19:27 »

good advise hawkwind

Will.

Offline EgonOlsen
« Reply #5 - Posted 2005-04-13 05:38:28 »

Quote
jest a thought....my first version of my game had several threads, worked like a charm until run on a fast machine  KABLAMM!!!
It gets even worse on hyper threading (or multiple) cpus...what works fine with just one cpu does strange things on HT machines.

Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (364 views)
2018-06-10 19:43:48

EgonOlsen (372 views)
2018-06-10 19:43:44

EgonOlsen (304 views)
2018-06-10 19:43:20

DesertCoockie (548 views)
2018-05-13 18:23:11

nelsongames (872 views)
2018-04-24 18:15:36

nelsongames (855 views)
2018-04-24 18:14:32

ivj94 (1307 views)
2018-03-24 14:47:39

ivj94 (426 views)
2018-03-24 14:46:31

ivj94 (1097 views)
2018-03-24 14:43:53

Solater (445 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!