Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
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 1653 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?


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 {
 } catch (InterruptedException ex) {
void windowClosed(WindowEvent ev) {
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()?

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.


jest a 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


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

jest a 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 (1599 views)
2018-06-10 19:43:48

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

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

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

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

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

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

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

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

Solater (911 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 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‑
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!