简体   繁体   English

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

[英]can I execute a particular block of code inside a method via thread

can I execute a particular block of code inside a method via thread.我可以通过线程在方法内执行特定的代码块吗? As for example例如

Class A{
    public void execute(){

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

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

yup all you have to do is implement runnable, then call that meathod inside of run()是的,您所要做的就是实现可运行,然后在 run() 中调用该肉类

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查线程是否在同步块或方法中? - How can I check if a thread is inside a synchronized block or method? 我可以将一段代码作为参数添加到方法中吗? - Can I add a block of code as parameter to a method? 如何在线程中停止块方法 DatagramSocket.receive() - how can i stop the block method DatagramSocket.receive() in a thread 如果我对静态方法进行类级别的锁定,并且如果一个线程执行了它,那么它将阻塞执行同一类的其他实例方法的其他线程吗? - If I make class level lock on static method & if one thread execute it so will it block other thread executing other instance method of same class? 我们如何才能在 java 的 main 方法中只执行一次特定的代码行? - How can we execute particular line of code only once in main method in java? 我不能使用在代码块中声明的变量调用方法 - i can't call a method with variables that are declared in a code block 如何在Blackberry中安排特定线程 - How can I schedule a particular thread in Blackberry IllegalMonitorStateException在其run方法内部的线程上调用wait()(无同步块) - IllegalMonitorStateException calling wait() on a thread inside its run method (with no synchronized block) 在同步方法内的线程中同步块会发生什么? - What would happen with a synchronized block in a thread which is inside a synchronized method? 同步线程一个简单的产生reduce问题我看不懂。 为什么同步块内的 Thread.sleep 使它工作不同? - synchronized thread a simple produce reduce problem I can not understand. why Thread.sleep inside synchronized block makes it work different?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM