[java] Java multi threads?

Started by
8 comments, last by Twixly 15 years, 8 months ago
Hey, well I just can´t wrap my head around threads.. I´ve read a few tutorials but when I try to write simple examples myself I dont get the expected result.. Here´s an easy example : class simpleClass implements Runnable { public void startMe() { new Thread( this ).start(); } public void run() { while(gameRunning) { // Here´s lots of things, like moving stuff around } } } class timerClass implements Runnable { public void startMe() { new Thread( this ).start(); } public void run() { while(gameRunning) { // Here´s a simple println of the time (HH:mm:ss) // I´ve done so its set to print time every 10 seconds } } } class mainClass { public static void main() { simpleClass one = new simpleClass(); timerClass two = new timerClass(); one.startMe(); two.startMe(); } } Problem now is that each thread seems to run once then nothing happens? Even tho each thread have a while loop in them, I was thinking they would both keep running their seperate loops untill I terminate the program? I just dont get it hehe.. as far as I can see this is how the simple tutorials do it, with small differences in functionailty ofcourse.. Can anyone please explain very simple how I get two or more threads running their seperate loops paralell? I am doing it this way just to learn threads by the way, using one to save variables to txt files and such, just to get a feeling for it. Thanks in advance.
Windows - Dev-Cpp 4.9.9 - SDL - SDL_Net
Advertisement
Don't know java so well anymore but I'm pretty sure that your main function also has to loop waiting for the 2 threads to finish. If main just executes as you've written it, your program will terminate before either thread has a chance to update multiple times.

-me
hmm.. I think the threads would keep running preventing the program from ending?
I am not sure tho.

Might be something else in my full code thats wrong.. but if someone could just confirm above or tell me if something looks wrong I would be gratefull and know to look elsewhere for the problem.
Windows - Dev-Cpp 4.9.9 - SDL - SDL_Net
I am pretty sure you need to call join from the main thread.
Calling join would be a good idea. But also, how have you created your gameRunning boolean? Seeing how boolans created as boolean gameRunning; default to false, this might be the problem.

While one thread is running, the java app should be kept alive by the jvm.

I'll see if I can test your code and find the problem later today. I'll post a solution when I have it.

Edit:
Ok, I've setup a rar archive at this location. This one works as you want (I think). Now it's just a question of comparing your setup to this one.

You should also take a look at the concurrency framework which has been around for a while now. It's a bit more high-level but offers some great extra features, such as thread pooling.

[Edited by - ractoc on August 20, 2008 3:14:48 AM]
Thanks alot, I´ve figured it out now. Seems I had one thread hogging to much of the CPU, because with any sort of sleep it worked fine.
Also noticed function called yield(); that surrenders time to other threads.

I´ve got my stuff working now tho! thanks again.
Windows - Dev-Cpp 4.9.9 - SDL - SDL_Net
Quote:Original post by Twixly
hmm.. I think the threads would keep running preventing the program from ending?
I am not sure tho.


They should keep the program running, as you don't seem to set them to be daemon threads.
I will keep threads in this thread :p
Anyone have time to give an example of how to make two threads share data? (ie. pass variables from one to the other and vice verce?)
I am not sure I get it. Keeping with the code from first post..

class mainClass
{
public static void main()
{
simpleClass one = new simpleClass();
timerClass two = new timerClass();

one.startMe();
two.startMe();
}
}

How do I send some data from inside simpleClass to the class timerClass?
If I declare timerClass in simpleClass, I just make a new instance of it don´t I?

Do I need to start timerClass from inside simpleClass? Like so:
class simpleClass
{

public void run()
{
timerClass timer = new timerClass();
timer.start();
}

}

......
And then using timerClass functions from in there they will run paralell to simpleClass? I just dont understand how you are suposed to think about this yet.
Windows - Dev-Cpp 4.9.9 - SDL - SDL_Net
You will probably want to pass a reference to the timerClass-object to the simpleClass-object, like:

class mainClass{public static void main(){timerClass two = new timerClass();simpleClass one = new simpleClass(two);one.startMe();two.startMe();}}


Then you could implement a setter method in the timerClass and call it from the simpleClass. However, at some place you'll need to use the synchronized keyword to avoid race conditions.
thanks I´ll try this when I come home.
Windows - Dev-Cpp 4.9.9 - SDL - SDL_Net

This topic is closed to new replies.

Advertisement