简体   繁体   中英

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?

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.

只需从run()方法调用它即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM