java 访问线程并在 Thread 类中运行一个方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12042976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:22:58  来源:igfitidea点击:

Access thread and run a method inside Thread class

javamultithreadingclass

提问by DannyF247

Here's my code:

这是我的代码:

public class DJ {
    static Thread djThread = new DJPlayThread();

    public static void play(){
        djThread.start();
    }
}

But once that thread is started, how can I run a method that is inside of the DJPlayThreadclass?

但是一旦该线程启动,我如何运行DJPlayThread类内部的方法?

Thanks.

谢谢。

回答by David Kroukamp

Here is a simple example of how to do what you ask:

这是一个简单的示例,说明如何执行您的要求:

public class ThreadControl {

    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable("MyRunnable");
        Thread thread = new Thread(myRunnable);
        thread.setDaemon(true);
        thread.start();

        myRunnable.whoAmI();//call method from within thread

        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
        }
        myRunnable.isStopped.set(true);//stop thread
    }

 static class MyRunnable implements Runnable {
        public String threadName;
        public AtomicBoolean isStopped=new AtomicBoolean(false);

        public MyRunnable() {
        }

        public MyRunnable(String threadName) {
            this.threadName = threadName;
        }

        public void run() {
            System.out.println("Thread started, threadName=" + this.threadName + ", hashCode="
                    + this.hashCode());

            while (!this.isStopped.get()) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                }
                System.out.println("Thread looping, threadName=" + this.threadName + ", hashCode="
                        + this.hashCode());
            }
        }

        public void whoAmI() {
            System.out.println("whoAmI, threadName=" + this.threadName + ", hashCode="
                    + this.hashCode());
        }

    }

}

回答by John Ericksen

public class DJ {
    private DJPlayThread djThread = new DJPlayThread();

    public void play() throws InterruptedException {
        djThread.start();

        Thread.sleep(10000);

        djThread.stopMusic();
    }

    public static void main(String[] args){
        try{
            new DJ().play();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class DJPlayThread extends Thread{

    private AtomicBoolean running = new AtomicBoolean(true);

    @Override
    public void run() {
        while(running.get()){
            System.out.println("Playing Music");
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void stopMusic(){
        //be careful about thread safety here
        running.set(false);
    }
}

Should print out:

应该打印出来:

Playing Music 
Playing Music 
Playing Music 
Playing Music 
Playing Music
Playing Music 
Playing Music 
Playing Music 
Playing Music 
Playing Music

Be very careful about the thread safety when exchanging information between threads. There are some weird things that happen when accessing and modifying variables across thread contexts.

在线程之间交换信息时要非常注意线程安全。在跨线程上下文访问和修改变量时会发生一些奇怪的事情。