Can I execute a specific block of code inside a method through a thread

Can I execute a specific block of code inside a method through a thread. For instance,

Class A{
    public void execute(){

    /* some code where threading is not required*/
    /* block of code which need to execute via thread */

    }
}
+5
source share
2 answers
class A {
    public void execute() {

        /* some code where threading is not required*/

        new Thread() {
            public void run() {
                /* block of code which need to execute via thread */
            }
        }.start();
    }
}
+14
source

yup all you have to do is implement runnable and then call this meathod inside run ()

+2
source

All Articles