Reputation: 678
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
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