run()方法不使用start()执行

时间:2017-02-12 10:10:42

标签: java multithreading

我想使用join()和isAlive函数。我创建了4个线程并在构造函数中初始化它们。我使用start方法启动了线程。它应该调用run()但是run方法没有执行。难道我做错了什么。请告诉我。提前谢谢。

  class MultiThreads implements Runnable {

    String name;
    Thread t;

    MultiThreads(String tname) {
        name=tname;
        t=new Thread(this.name);
        System.out.println("Thread name: " + t);
        t.start();              //executing run()
    }

    public void run(){

       try{
         for(int i=1;i<11;i++){
            System.out.println("Thread-"+name+ ": " + i);
            t.sleep(500);           
            }
       }catch(Exception ie){
           System.out.println("An error has occurred");
        }
    }
 }

public class JoinAlive {

    public static void main(String args[]) {

        //Creating New Threads by calling constructor.

        MultiThreads t1=new MultiThreads("One");``
        MultiThreads t2=new MultiThreads("Two");
        MultiThreads t3=new MultiThreads("Three");
        MultiThreads t4=new MultiThreads("Four");

        System.out.println();

        System.out.println("Thread-One active: " + t1.t.isAlive());
        System.out.println("Thread-Two active: " + t2.t.isAlive());
        System.out.println("Thread-Three active: " + t3.t.isAlive());
        System.out.println("Thread-Four active: " + t4.t.isAlive());

       try{
           System.out.println();
           System.out.println(" Waiting for One");
           t1.t.join();
           System.out.println(" Waiting for Two");
           t2.t.join();
         }catch(InterruptedException ie){
             System.out.println("An error occurred");
          }

          System.out.println();

          System.out.println("Thread-One active: " + t1.t.isAlive());
          System.out.println("Thread-Two active: " + t2.t.isAlive());
          System.out.println("Thread-Three active: " + t3.t.isAlive());
          System.out.println("Thread-Four active: " + t4.t.isAlive());
     }
 }

3 个答案:

答案 0 :(得分:2)

问题是{p> t=new Thread(this.name); 您为该主题命名,但未提供关联的目标Runnable实例。

只需使用此构造函数:

public Thread(Runnable target, String name) 

以这种方式:

t=new Thread(this,this.name); 

答案 1 :(得分:0)

在代码中创建Thread对象是一个没有目标的线程。

你正在使用这个:

t=new Thread(this.name);

你应该用这个来初始化你的线程:

t=new Thread(this, this.name);

它会将Runnable的run方法链接到Thread的start方法。

希望这有帮助!

答案 2 :(得分:0)

使用此编辑的Code`类MultiThreads实现Runnable {

String name;
Thread t;

MultiThreads(String tname) {
    name=tname;
    t=new Thread(this.name);
    System.out.println("Thread name: " + t);
    t.start();              //executing run()
}

public void run(){

   try{
     for(int i=1;i<11;i++){
        System.out.println("Thread-"+name+ ": " + i);
        t.sleep(500);           
        }
   }catch(Exception ie){
       System.out.println("An error has occurred");
    }
}

}

public class JoinAlive {

public static void main(String args[]) {

    //Creating New Threads by calling constructor.

    Thread t1=new Thread(new MultiThreads("One"));
    Thread t2=new Thread(new MultiThreads("Two"));
    Thread t3=new Thread(new MultiThreads("Three"));
    Thread t4=new Thread(new MultiThreads("Four"));

    System.out.println();

    System.out.println("Thread-One active: " + t1.isAlive());
    System.out.println("Thread-Two active: " + t2.isAlive());
    System.out.println("Thread-Three active: " + t3.isAlive());
    System.out.println("Thread-Four active: " + t4.isAlive());

   try{
       System.out.println();
       System.out.println(" Waiting for One");
       t1.join();
       System.out.println(" Waiting for Two");
       t2.join();
     }catch(InterruptedException ie){
         System.out.println("An error occurred");
      }

      System.out.println();

      System.out.println("Thread-One active: " + t1.isAlive());
      System.out.println("Thread-Two active: " + t2.isAlive());
      System.out.println("Thread-Three active: " + t3.isAlive());
      System.out.println("Thread-Four active: " + t4.isAlive());
 }

}`