Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

wicked_wesley

[java] Threads 'n methods :)

This topic is 5395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there, I'm not really new to Java, I've been busy with it for quite some time now I'm trying to make a threaded server now, but I can't figure something out about threads. I have a main thread/class and a thread, in my thread I call a method in the main class. My question is; Will the method be executed in the main thread or in the thread? For those who don't understand my question: I have a main thread, kinda like this:
//Main class
public class Main
  {
  public static void main(String args[])
    {
    ThreadA thread=new ThreadA(this);
    thread.start();
    }

  public void doAlotOfWork(int number)
    {
    //Do some hard calculations, etc. 
    }
  }
   
And a thread class, like this:
//Constructor
public ThreadA(Main main)
  {
  this.mainThread=main;
  }
public void run()
  {
  mainThread.doAlotOfWork(10);
  }
   
**Note** I have made the code abit shorter, so it's easier to read **Note** Which thread will do all the work? ThreadA; because he calls the funtion so he does the job. Or Main Thread; The function is in the Main class, so he does the job. I have tried some things with the threads, and as far as I can see, ThreadA will do all the work, but I'm not sure, so can anybody tell me if the Main thread or the ThreadA will do all the work? Thank you for your time, Wesley [edited by - wicked_wesley on January 11, 2004 2:30:44 PM] [edited by - wicked_wesley on January 11, 2004 2:32:12 PM]

Share this post


Link to post
Share on other sites
Advertisement
The thread that calls a method will perform the work in the method, no matter where the method is located.

Does your code really work though? You are calling ThreadA''s constructor with "this" as an argument from a static context. I think you need to create an instance of the class Main and pass that to the constructor of ThreadA, like this:

public static void main(String args[])
{
Main m = new Main(); // instantiate Main, as no "this" instance exists for a method being called from a static context
ThreadA thread=new ThreadA(m);
thread.start();
}

Share this post


Link to post
Share on other sites
Just as a postscript, I thought I'd add a suggestion to consider using the "implements Runnable" style rather than "extends Thread". I find that it's more flexible and easier to wrap my head around

Thus:

public class Main implements Runnable {
public static void main(String[] args) {
new Thread(new Main()).start();
// or you could maintain the references instead of using
// temporaries, if you need to control the thread later
}
// implementation of Runnable
public void run() {
doALotOfWork(10);
// with this style you need to figure out how to pass in your arguments :/
}
}
// no ThreadA class needed

Of course, if you were planning to add other methods to ThreadA, or override something other than run(), then you'll probably want to stick with the style you were using.

[edited by - Zahlman on January 11, 2004 7:25:20 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by HenryApe
The thread that calls a method will perform the work in the method, no matter where the method is located.

Does your code really work though? You are calling ThreadA''s constructor with "this" as an argument from a static context. I think you need to create an instance of the class Main and pass that to the constructor of ThreadA, like this:

public static void main(String args[])
{
Main m = new Main(); // instantiate Main, as no "this" instance exists for a method being called from a static context
ThreadA thread=new ThreadA(m);
thread.start();
}



Yes I know My full code is quite different, so I quickly made this one

And to Zahlman, yes I''m making a class for the thread, and will manage it wit another class, but that would be too complicated to explain here

Thank you for your help and suggestions
Wesley

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!