Java 创建多线程循环

Java 创建多线程循环,java,multithreading,loops,Java,Multithreading,Loops,抱歉,如果这是一个基本的问题,但我一直在考虑做多个sprite循环,我第一次尝试在main中创建两个线程,都使用while(true)循环。我的意图是:让两个线程同时循环。但是,当我运行程序时,它似乎会中断执行流,第二个循环不会在新线程中执行,而是在程序卡在线程的第一个无限while()循环时停止。我认为它仍然只是执行主线程,而不是启动一个新线程然后继续 我试过两种方法: 一次使用线程: public class Zzz { /** * @param args the com

抱歉,如果这是一个基本的问题,但我一直在考虑做多个sprite循环,我第一次尝试在main中创建两个线程,都使用while(true)循环。我的意图是:让两个线程同时循环。但是,当我运行程序时,它似乎会中断执行流,第二个循环不会在新线程中执行,而是在程序卡在线程的第一个无限while()循环时停止。我认为它仍然只是执行主线程,而不是启动一个新线程然后继续

我试过两种方法:

一次使用线程:

public class Zzz {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        r1 r = new r1();
        r2 a = new r2();
        r.start();
        a.start();
    }
}

public class r1 extends Thread {

    @Override
    public void start() {
        while(true) {
            System.out.println("r1");
            try {
                this.sleep(100);
            } catch (Exception ex) {

            }
        }
    }

}

public class r2 extends Thread {

    @Override
    public void start() {
        while(true) {
            System.out.println("r2");
                        try {
                this.sleep(100);
            } catch (Exception ex) {

            }
        }
    }

}
并使用Runnable:

public class Zzz {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        r1 r = new r1();
        r2 a = new r2();
        r.run();
        a.run();
    }
}

public class r1 implements Runnable {

    @Override
    public void run() {
        while(true) {
            System.out.println("r1");
            try {
                Thread.sleep(100);
            } catch (Exception ex) {

            }
        }
    }

}

public class r2 implements Runnable {

    @Override
    public void run() {
        while(true) {
            System.out.println("r2");
                        try {
                Thread.sleep(100);
            } catch (Exception ex) {

            }
        }
    }

}

但是没有用。它总是卡在R1。有什么想法吗?我在谷歌上搜索了一下线程,但我在任何地方都找不到它。

您需要重写run方法&在runnable的情况下,您需要创建线程的实例

public class MyThread extends Thread{
    @Override
    public void run() {
        while(true) {
            System.out.println("My Thread running");
        }

}
对于
Runnable

class MyRunnable implements Runnable{

             public void run(){
                         System.out.println("I am executing by Thread: " + Thread.currentThread().getName());
             }
 }


要启动线程,您需要从
Runnable
s创建两个
Thread
s并启动它们:

Thread t1 = new Thread(r);
Thread t2 = new Thread(a);
t1.start();
t2.start();

将r1和r2类定义为:

public class Thread1 extends Thread {

@Override
public void run() {
    System.out.println("r1");
    try {
       this.sleep(100);
    } catch (Exception ex) {

    }        
 }

}

public class Thread2 extends Thread {

@Override
public void run() {
    System.out.println("r2");
    try {
      this.sleep(100);
    } catch (Exception ex) {
    }
}
}


public class ThreadTester {

    public static void main(String[] args) {
        Thread1 r = new Thread1();
        Thread2 a = new Thread2();
        r.start();
        a.start();
    }
}
使用Runnable:

public class HelloRunnable implements Runnable {

    public void run() {
        System.out.println("Hello from a thread!");
    }

    public static void main(String args[]) {
        (new Thread(new HelloRunnable())).start();
    }

}
查看更多信息

public class HelloRunnable implements Runnable {

    public void run() {
        System.out.println("Hello from a thread!");
    }

    public static void main(String args[]) {
        (new Thread(new HelloRunnable())).start();
    }

}