我可以通过线程在方法内执行特定的代码块

时间:2011-07-19 07:21:59

标签: java multithreading

我可以通过线程在方法内执行特定的代码块。例如

Class A{
    public void execute(){

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

    }
}

2 个答案:

答案 0 :(得分:17)

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();
    }
}

答案 1 :(得分:2)

是的,所有你要做的就是实现runnable,然后在run()中调用那个meathod