Marcel_marcel1991
Marcel_marcel1991

Reputation: 678

How can I call a different method than run() from a Thread

Let's say I have a post() and a get() method that I want to run from a different thread. Is it possible to do this with just one class that extends Thread, where those methods are not in the run() method?

I thought of this:

XYThread xy = new XYThread();
xy.start();
xy.post();
xy.get();

But in this case, would it still would be multithreading after the run() method is finished?

Upvotes: 1

Views: 84

Answers (2)

user2864740
user2864740

Reputation: 61985

Do the work inside the run method; and call whatever methods you wish to call.

However, run and only run is called "within" the thread. In the posted code both post and get are not executed within the context of the xy thread; but instead in the context of the current thread.

It is also possible to supply a Runnable to a thread, which once again has its run method invoked, such that the Thread does not need to be sub-classed. And yes, at some level this means creating different classes or otherwise embedding logic.

Upvotes: 2

user207421
user207421

Reputation: 311055

Just call it from the run() method.

Upvotes: 0

Related Questions