简体   繁体   English

为什么一个线程阻止另一个线程运行?

[英]Why does one thread stop the other thread from running?

I was going to use threads for each sound in a game engine I'm making. 我要为正在制作的游戏引擎中的每种声音使用线程。 The problem is, whenever I make a new thread that has a while(true) statement, the other thread stops running. 问题是,每当我创建一个具有while(true)语句的新线程while(true) ,另一个线程就会停止运行。

I made a class to test this, and it only prints "goodbye", not "hello". 我做了一个类来测试这一点,它只显示“再见”,而不显示“你好”。 I was wondering how to make the two threads run at the same time. 我想知道如何使两个线程同时运行。

public class testor {
    public static void main(String args[]){
        testor test=new testor();
        test.runTest();
    }
    class threadTest implements Runnable{

        @Override
        public void run() {
            while(true){
                System.out.println("goodbye");
            }

        }

    }
    public void runTest(){
        threadTest test=new threadTest();
        test.run();
        while(true){
            System.out.println("hello");
        }
    }
}

Since you are doing test.run(); 由于您正在执行test.run(); you are only calling the method of that class but not starting the thread. 您只是在调用该类的方法,而没有启动线程。

So in order to answer your question: there is no such a thread stopping the other thread from running? 因此,为了回答您的问题:没有这样的线程阻止另一个线程运行吗? because you have only one Thread that is looping for ever and printing the message System.out.println("goodbye"); 因为您只有一个线程永远循环并打印消息System.out.println("goodbye");

If that method is not looping for ever, it would return to the runTest method and then you would see the System.out.println("hello"); 如果该方法永远不会循环,它将返回到runTest方法,然后您将看到System.out.println("hello"); runTest

Summary: 摘要:

For starting a Thread use the Thread::start method and not the run . 要启动Thread使用Thread :: start方法,而不要使用run

Using (new ThreadTest()).run() does not start a new Thread , but just invokes the run() method in the current thread. 使用(new ThreadTest()).run()不会启动新的Thread ,而只是在当前线程中调用run()方法。

To run the code in a separate thread do: 要在单独的线程中运行代码,请执行以下操作:

(new Thread(new ThreadTest())).start();

That's because you're not creating a new thread. 那是因为您没有创建新线程。 Just naming a class something containing "thread" will not make it a thread, and a Runnable is no thread - it's a class like any other, with no special semantics or behaviour. 仅给一个类命名包含“线程”的东西就不会使其成为线程,而Runnable则不是线程-它是一个与其他任何类一样的类,没有特殊的语义或行为。

It's only special in that you can pass it to a Thread for execution. 您可以将其传递给Thread以执行,这是唯一的特殊之处。

public class Testor {
    public static void main(String args[]){
        Testor test=new Testor();
        test.runTest();
    }

    class MyRunnable implements Runnable{
        @Override
        public void run() {
            while(true){
                System.out.println("goodbye");
            }
        }
    }

    public void runTest(){
        Thread testThread = new Thread(new MyRunnable());
        testThread.start();
        while(true){
            System.out.println("hello");
        }
    }
}

You should probably also adhere to the Java coding standards regarding your class and variable names if you do not want your code to look like an alien when combined with most other existing Java code. 如果您不希望将代码与大多数其他现有Java代码结合使用时看起来像外星人,则可能还应该遵守有关类和变量名称的Java编码标准。

Additionally, multithreading is more than just being able to start a new thread. 此外,多线程不仅仅是能够启动新线程。 You should also read about synchronisation issues - it's more complicated to do correctly than you might imagine. 您还应该阅读有关同步问题的信息-正确地进行操作比您想象的要复杂。

Your run method contains an infinite loop. 您的run方法包含一个无限循环。 The runTest() method creates the thread which means you'll have 2 execution stacks the main stack, and the runnable threadTest stack. runTest()方法创建线程,这意味着您将有2个执行堆栈(主堆栈)和可运行的threadTest堆栈。 since you're running the thread method first that contains an infinite loop, you'll always get the output "good Bye" . 因为您首先运行的是包含无限循环的线程方法,所以总会得到输出"good Bye"

Remove the infinite loop from run() method. run()方法中删除无限循环。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM