在不等待的情况下调用一个函数 [英] Call a function without waiting for it
本文介绍了在不等待的情况下调用一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好我想知道是否有方法调用函数/方法(最好是在Python或Java中),并继续执行而不必等待。
示例:
$ b $
def a():
b()#call a function,b()
returnsomething
def b():
#需要很长时间的东西
解决方案
在新线程中运行它。在java中学习多线程此处和python多线程 Java示例:
错误的方式...通过子类化Thread
new Thread(){
public void run (){
YourFunction(); //调用你的函数
}
} .start();
通过提供Runnable实例的正确方法
Runnable myrunnable = new Runnable(){
$ p $时调用它p>
public void run(){
YourFunction(); //调用你的函数
}
}
新线程(myrunnable).start(); //当你需要运行函数
Hi I was wondering if there was a way of calling a function/method (preferably in Python or Java) and continue execution without waiting for it.
Example:
def a(): b() #call a function, b() return "something" def b(): #something that takes a really long time
解决方案Run it in a new thread. Learn about multithreading in java here and python multithreading here
Java example:
The WRONG way ... by subclassing Thread
new Thread() { public void run() { YourFunction();//Call your function } }.start();
The RIGHT way ... by supplying a Runnable instance
Runnable myrunnable = new Runnable() { public void run() { YourFunction();//Call your function } } new Thread(myrunnable).start();//Call it when you need to run the function
这篇关于在不等待的情况下调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文