无法在多线程之后执行任何操作时获得正确的时间

时间:2017-01-19 14:29:49

标签: java multithreading

为什么我不能得到正确的输出? 这是我的代码:

=======

1。主

public class ThreadDebut {
  public static void main(String args[]) {
     PrintDemo PD = new PrintDemo();
     List<ThreadDemo> th = new ArrayList<ThreadDemo>();
     ThreadDemo T1 = new ThreadDemo( "Thread - 1 ", PD );
     ThreadDemo T2 = new ThreadDemo( "Thread - 2 ", PD );


     T1.start();
     th.add(T1);
     T2.start();
     th.add(T2);

     for(ThreadDemo t : th) {
         try {
            t.join();
         }catch( Exception e) {
           System.out.println("Interrupted");
         }
     }
   System.out.println("\n\n\n");
   }
}

2.Thread and method

 class PrintDemo {
   public void printCount() {
      try {
         for(int i = 5; i > 0; i--) {
            System.out.println("Counter   ---   "  + i );
         }
      }catch (Exception e) {
         System.out.println("Thread  interrupted.");
      }
   }
}

class ThreadDemo extends Thread {
   private Thread t;
   private String threadName;
   PrintDemo  PD;

       ThreadDemo( String name,  PrintDemo pd) {
      threadName = name;
          PD = pd;
       }

       public void run() {
          synchronized(PD) {
             PD.printCount();
          }
          System.out.println("Thread " +  threadName + " exiting.");
       }

   public void start () {
      System.out.println("Starting " +  threadName );
      if (t == null) {
         t = new Thread (this, threadName);
         t.start ();
      }
   }
}

我的意思是我想在两个线程完成执行后得到这个 System.out.println(“\ n \ n \ n”); 。但是,所有我得到的是在线程1的同步之前的随机。我已经做thread.join()但失败了。为什么?有人请给我解释...谢谢和问候!!!

2 个答案:

答案 0 :(得分:1)

您正在尝试同时创建子类并使用委托。你essentaly为每个线程创建两个线程。您应该重写Thread的方法,而不是使用委托。像这样:

static class ThreadDemo extends Thread {
    private String threadName;
    PrintDemo PD;

    ThreadDemo (String name, PrintDemo pd) {
        threadName = name;
        PD = pd;
    }

    @Override
    public void run() {
        synchronized (PD) {
            PD.printCount();
        }
        System.out.println("Thread " + threadName + " exiting.");
    }

    @Override
    public void start() {
        System.out.println("Starting " + threadName);
    }
}

答案 1 :(得分:0)

在您的情况下,您没有使用ThreadDemo类作为Thread的子类。您在ThreadDemo中创建另一个Thread t。如果要使用join方法,则必须添加适配器方法,如下所示:

static class ThreadDemo extends Thread {
    private Thread t;
    private String threadName;
    PrintDemo PD;

    ThreadDemo(String name, PrintDemo pd) {
        threadName = name;
        PD = pd;
    }

    public void run() {
        synchronized (PD) {
            PD.printCount();
        }
        System.out.println("Thread " + threadName + " exiting.");
    }

    public void start() {
        System.out.println("Starting " + threadName);
        if (t == null) {
            t = new Thread(this, threadName);
            t.start();
        }
    }

    public void join1() {
        try {
            t.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}