2011-01-22 58 views
-2

我在Java中使用Thread。我创建扩展Thread类和它的罚款,但问题是,当调用此Thread,我不知道这个类的实例的数量,因为用户必须输入这个号码。例如:如何在Java中使用线程?

multiThread multiThreadInstance = new multiThread(/* number entered from user */); 
multiThreadInstance.start(); 

这将调用这个线程一次,但如果我写:

multiThread multiThreadInstance1 = new multiThread(/* number entered from user */) 
multiThreadInstance1.start() 
multiThread multiThreadInstance2 = new multiThread(/* number entered from user */) 
multiThreadInstance2.start() 

这将调用两次在同一时间,等等。

如果我把它放在一个for循环,然后如果用户输入3,例如,然后start1运行时,当start1饰面,start2运行时,当start2饰面,start3运行。我需要让这些线程的实例同时运行。我怎样才能做到这一点?

+0

很抱歉,如果我错过了一些东西,但我不知道太明白你的问题。首先是从用户输入的*号码*在您的问题中很重要?你的问题中的所有例子似乎都在起作用。你可以发布你的循环实现的代码,并告诉我们什么不工作或你不知道该怎么做。 – gabuzo 2011-01-22 08:53:29

回答

1

您需要使用Java的高水平并发工具来做到这一点。看看倒数计时器和执行者。以下是可以做你想要的代码。我建议你阅读java并发实用程序。

import java.util.concurrent.*; 

public class ConcurrentTimer { 
    private ConcurrentTimer() { } // Noninstantiable 

    public static long time(Executor executor, int concurrency, 
      final Runnable action) throws InterruptedException { 
     final CountDownLatch ready = new CountDownLatch(concurrency); 
     final CountDownLatch start = new CountDownLatch(1); 
     final CountDownLatch done = new CountDownLatch(concurrency); 

     for (int i = 0; i < concurrency; i++) { 
      executor.execute(new Runnable() { 
       public void run() { 
        ready.countDown(); // Tell timer we're ready 
        try { 
         start.await(); // Wait till peers are ready 
         action.run(); 
        } catch (InterruptedException e) { 
         Thread.currentThread().interrupt(); 
        } finally { 
         done.countDown(); // Tell timer we're done 
        } 
       } 
      }); 
     } 

     ready.await();  // Wait for all workers to be ready 
     long startNanos = System.nanoTime(); 
     start.countDown(); // And they're off! 
     done.await();  // Wait for all workers to finish 
     return System.nanoTime() - startNanos; 
    } 
} 

上面提供的代码示例的可运行版本:(编辑)

import java.util.concurrent.*; 

public class ConcurrentTimer { 

    public static void main(String[] args) { 


     try { 
      Runnable action = new Runnable() { 
        public void run() { 

         System.out.println("Thread Running"); 

        } 
       }; 

      time (3, action); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 


    private ConcurrentTimer() { } // Noninstantiable 

    public static long time(int concurrency, 
      final Runnable action) throws InterruptedException { 

     Executor executor = Executors.newFixedThreadPool(concurrency); 


     final CountDownLatch ready = new CountDownLatch(concurrency); 
     final CountDownLatch start = new CountDownLatch(1); 
     final CountDownLatch done = new CountDownLatch(concurrency); 

     for (int i = 0; i < concurrency; i++) { 
      executor.execute(new Runnable() { 
       public void run() { 
        ready.countDown(); // Tell timer we're ready 
        try { 
         start.await(); // Wait till peers are ready 
         action.run(); 
        } catch (InterruptedException e) { 
         Thread.currentThread().interrupt(); 
        } finally { 
         done.countDown(); // Tell timer we're done 
        } 
       } 
      }); 
     } 

     ready.await();  // Wait for all workers to be ready 
     long startNanos = System.nanoTime(); 
     start.countDown(); // And they're off! 
     done.await();  // Wait for all workers to finish 
     return System.nanoTime() - startNanos; 
    } 
} 
2

我怀疑您意外覆盖了start()方法Thread。请确保它是run()方法你覆盖。

如果您用自己的实现覆盖了start()方法,那么您将“移除Thread类的魔法”。奇妙之处在于start()它开始在一个新的线程run()方法的执行,所以保持你自己的代码中run()

+0

我把run方法我自己的代码,当我说multiThreadInstance.start()方法run工作 – 2011-01-22 07:49:20

+0

你有你的run方法的任何同步?例如`wait` /`notify`。 – aioobe 2011-01-22 07:52:59