在工作中训练,我正在写一个Java(其中我有经验0)程序应满足以下条件:如何添加的Java多线程
写可复制分布式计算应用
程序其包含M个随机数的列表创建中央“调度”对象
创建从调度器检索数N处理器线程然后循环,请求另一个号码
之前多次如果调度程序中没有可用的号码,请等待请求另一个号码。
如果没有更多的数字,所有的线程应该结束。
到目前为止,我创建了一个随机数组的对象,但我真的不知道如何进行多线程。有人可以引导我通过它吗?这是我迄今为止,以及注释表明伪代码。
public class ThreadDemo extends Thread
{
//create new array of arbitrary size 5
static int SIZE = 5;
static int[] myIntArray = new int[SIZE];
public ThreadDemo()
{
start();
}
class RunnableThread implements Runnable {
Thread runner;
public RunnableThread() {
}
public RunnableThread(String threadName) {
runner = new Thread(this, threadName); // (1) Create a new thread.
System.out.println(runner.getName());
runner.start(); // (2) Start the thread.
}
public void run() {
//Display info about this particular thread
System.out.println(Thread.currentThread());
}
}
public static void main(String[] args)
{
for(int i=0; i<SIZE; i++)
{
myIntArray[i] = (int)(Math.random() * 10);
}
ThreadDemo scheduler = new ThreadDemo();
//create M processor threads that retrieve number from scheduler
//for(int i=0; i<SIZE; i++)
//
//if no threads available
//make the scheduler thread wait() ??
//if empty
//stop() the scheduler thread ??
}
}
任何人都可以引导我走向正确的方向吗?
谢谢!
这个问题的说法有点奇怪。如果确切地有M个数字和M个线程,那么每个线程将选取一个数字,然后终止。最后两个约束是什么? – Tudor 2012-07-06 13:32:22
对不起,我犯了一个错字。实际上有M个随机数和N个处理器线程。 – TimeBomb006 2012-07-06 13:57:54