Sign in to follow this  
Twixly

[java] Java multi threads?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this