Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么一个线程阻止另一个线程运行?_Java_Multithreading - Fatal编程技术网

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

Java 为什么一个线程阻止另一个线程运行?,java,multithreading,Java,Multithreading,我打算在我制作的游戏引擎中为每个声音使用线程。问题是,每当我创建一个包含while(true)语句的新线程时,另一个线程就会停止运行 我制作了一个类来测试这个,它只打印“再见”,而不是“你好”。我想知道如何使两个线程同时运行 public class testor { public static void main(String args[]){ testor test=new testor(); test.runTest(); } cla

我打算在我制作的游戏引擎中为每个声音使用线程。问题是,每当我创建一个包含
while(true)
语句的新线程时,另一个线程就会停止运行

我制作了一个类来测试这个,它只打印“再见”,而不是“你好”。我想知道如何使两个线程同时运行

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

因为您正在执行
test.run()您只调用该类的方法,而不是启动线程

因此,为了回答您的问题:没有这样一个线程阻止另一个线程运行?因为您只有一个线程永远循环并打印消息
System.out.println(“再见”)

如果该方法永远不循环,它将返回到
runTest
方法,然后您将看到
System.out.println(“hello”)

总结:
要启动
线程
请使用该方法,而不要使用
运行

,因为您正在执行
测试。运行()您只调用该类的方法,而不是启动线程

因此,为了回答您的问题:没有这样一个线程阻止另一个线程运行?因为您只有一个线程永远循环并打印消息
System.out.println(“再见”)

如果该方法永远不循环,它将返回到
runTest
方法,然后您将看到
System.out.println(“hello”)

总结: 要启动
线程
,请使用该方法,而不要使用
(new ThreadTest())运行
,。
不会启动新的
线程
,只会在当前线程中调用
运行()
方法

要在单独的线程中运行代码,请执行以下操作:

(new Thread(new ThreadTest())).start();
使用
(new ThreadTest()).run()
不会启动新的
线程
,只会在当前线程中调用
run()
方法

要在单独的线程中运行代码,请执行以下操作:

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

这是因为您没有创建新线程。仅仅命名一个包含“thread”的类并不能使它成为线程,
Runnable
也不是线程——它和其他类一样,没有特殊的语义或行为

它的特殊之处在于,您可以将其传递给
线程执行

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");
        }
    }
}
如果您不希望代码与大多数其他现有Java代码结合时看起来像外星人,那么您可能还应该遵守关于类和变量名的Java编码标准


此外,多线程不仅仅是能够启动一个新线程。您还应该阅读有关同步问题的内容—正确执行同步比您想象的要复杂。

这是因为您没有创建新线程。仅仅命名一个包含“thread”的类并不能使它成为线程,
Runnable
也不是线程——它和其他类一样,没有特殊的语义或行为

它的特殊之处在于,您可以将其传递给
线程执行

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");
        }
    }
}
如果您不希望代码与大多数其他现有Java代码结合时看起来像外星人,那么您可能还应该遵守关于类和变量名的Java编码标准


此外,多线程不仅仅是能够启动一个新线程。您还应该阅读有关同步问题的内容—正确执行同步比您想象的要复杂。

您的
run
方法包含一个无限循环。
runTest()
方法创建线程,这意味着您将有两个执行堆栈:主堆栈和runnable
threadTest
堆栈。 由于您首先运行的是包含无限循环的thread方法,因此您将始终获得输出
“再见”


run()
方法中删除无限循环。

您的
run
方法包含无限循环。
runTest()
方法创建线程,这意味着您将有两个执行堆栈:主堆栈和runnable
threadTest
堆栈。 由于您首先运行的是包含无限循环的thread方法,因此您将始终获得输出
“再见”


run()
方法中删除无限循环。

您不仅仅是通过调用
run()
来启动线程。在这里看一看:oooooohhhhhhhh,lol。谢谢如果您的问题得到了回答,请将答案标记为答案(一句话中有很多答案)。您不仅仅是通过调用
run()
来启动线程。在这里看一看:Ooooo-hhhhhhhh,lol。谢谢如果你的问题得到回答,将答案标记为答案(一句话中有很多答案)。是的,但是OP的问题的答案是什么?好的,但我的意思是完整的答案将描述如何实际开始一个线程。是的,但是OP的问题的答案是什么?好的,好的,但我的意思是,完整的答案将描述如何实际启动线程。