[java] Threads 'n methods :)

Started by
4 comments, last by wicked_wesley 20 years, 3 months ago
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]
Have a nice dayWesley
Advertisement
ThreadA does the work.
Ahhh, thought so

Thanks for your time and help!

Wesley
Have a nice dayWesley
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();} 
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]
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
Have a nice dayWesley

This topic is closed to new replies.

Advertisement