Call a function without waiting for it

前端 未结 6 1385
粉色の甜心
粉色の甜心 2021-02-02 00:59

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:



        
6条回答
  •  隐瞒了意图╮
    2021-02-02 01:27

    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
    

提交回复
热议问题