• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple question about threads

 
Thomas Touzimsky
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

still a newbie here. I would like to pose a question about threads which might seem a bit trivial...

I know that there is no absolute guarantee for which thread will be executed during runtime if there are several threads are set runnable and share CPU time. However, can I be sure that as soon as a thread is set runnable, it will be run within a certain period of time? Or could it happen that I have to wait, let's say, several seconds for a certain thread to be run?

Thanks!
 
Alexandr Shvedov
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There some states in which a thread can be in. They are - running(hold the CPU), ready(ready to start running) and various other states.
When a thread becames 'ready' it can be moved to running state only by thread scheduler. You can not guaranty that it happens in a sec, in a day or somewhere in the future, because it is up to scheduler to decide.
 
Thomas Touzimsky
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, ok. Thanks for that. I am currently thinking about a simple game to test my freshly learned Java skills on, and Tetris seems to be a good example to practise since it's simple but still has all the things other games have as well (maybe in lesser numbers, but still...)

So I thought, the best way to move a block from top to the bottom of the screen would be a thread which, while the game's own state is "running", moves the current block down one step (unless it would hit the bottom or another block), sleeps for 1000ms, wakes up again, moves the block, and so on.

However, this wouldn't be the best approach if that thread wakes up after 1000ms but has to wait another second or so until the scheduler decides it would be time for the thread to run, although I guess there won't be too many other threads in my Tetris example which could get in its way...


[edit] I know this post would also fit in the Game forum, but I thought that the question also applies outside of that context, so please bear with me.
[ February 06, 2007: Message edited by: Thomas Touzimsky ]
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if you intended this already but if you do use threads don't code something like height -=1 every 1000ms for the falling block instead calculate where the block should be by looking at the current time from start of fall, that way on a CPU limited machine your animation will always complete in the same time just drop frames. Obviously this makes coding a bit trickier (collison detction etc) but a nicer user experience (well I think) I used something similar on a snooker simulation and it worked well on a wide range of machines.

Although you can't guarantee your thread being responsive in practice modern JVM's on modern PC's are (particularly with all those new dual core Pu's arriving ;-) ), though there are some extreeme cases like at least one Oracle database JVM that does threads effectively sequentially ;-)
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!