3

抱歉,如果这是一个基本问题,但我一直在考虑做多个精灵循环,并且我第一次尝试在 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 上。有什么想法吗?我已经用谷歌搜索并环顾四周,但我无法在任何地方找到它。

4

3 回答 3

2

您需要覆盖 run 方法,如果是可运行的,您需要创建 Thread 的实例

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

  Thread mythread = new MyThread();
  mythread.setName("T1");
  Thread myrunnable = new Thread(new MyRunnable());
  myrunnable.start();
于 2012-06-06T06:00:24.177 回答
2

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

Thread t1 = new Thread(r);
Thread t2 = new Thread(a);
t1.start();
t2.start();
于 2012-06-06T06:02:43.053 回答
2

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

使用可运行:

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

}

检查java 文档以获取更多信息

于 2012-06-06T06:08:34.590 回答