Java线程启动其他线程,每个线程都写入数组

Java线程启动其他线程,每个线程都写入数组,java,arrays,multithreading,thread-sleep,notify,Java,Arrays,Multithreading,Thread Sleep,Notify,对Java来说是完全陌生的,但是我必须以某种方式让它工作起来。我希望你们能让我走上正确的道路 程序必须创建N个线程和N个元素的数组。第一个线程应该向数组中写入一个随机数(这里-resultArray),调用(或创建)下一个线程(,它将执行相同的操作),然后休眠,直到最后一个线程通知所有其他休眠线程 那么,到目前为止我做得对吗?如何使运行()访问演示的resultArray并写入线程的随机数? 另外,run()如何到达(属于threadList的)其他线程以通知它们 多谢各位 public cla

对Java来说是完全陌生的,但是我必须以某种方式让它工作起来。我希望你们能让我走上正确的道路

程序必须创建N个线程和N个元素的数组。第一个线程应该向数组中写入一个随机数(这里-
resultArray
),调用(或创建)下一个线程(,它将执行相同的操作),然后休眠,直到最后一个线程通知所有其他休眠线程

那么,到目前为止我做得对吗?如何使
运行()
访问演示的resultArray并写入线程的随机数? 另外,
run()
如何到达(属于
threadList
的)其他线程以通知它们

多谢各位

public class gijos extends Thread {

    private int length; 
    private int position;

    public gijos(int arrPos) { 
      position = arrPos;
    } 

    public int getPosition(){
       return position;
    }

    public void run() {   
             Random rand = new Random();
             int  n = rand.nextInt(51) + 1;
    }

公共类演示{
公共静态void main(字符串[]args){
System.out.println(“数组长度/线程计数:”);
扫描仪s=新的扫描仪(System.in);
int N=s.nextInt();
int[]resultArray=新int[N];
GIJO[]线程列表=新的GIJO[N];
对于(int i=0;i
这里有一个示例工人类:

public class ArrayWorker implements Runnable {
    private static final List<ArrayWorker> threadList = new LinkedList<>();
    private static final Random rnd = new Random(System.currentTimeMillis());

    private final int[] array;
    private final int index;


    public ArrayWorker(final int[] array, final int index) {
        if (index > array.length - 1) {
            throw new IndexOutOfBoundsException(String.format("%d", index - array.length));
        }

        this.array = array;
        this.index = index;
        System.out.println(this + " has been created");
    }


    @Override
    public void run() {
        System.out.println(this + " run()");
        this.array[this.index] = rnd.nextInt(100);

        if (index < array.length - 2) {
            final ArrayWorker worker = new ArrayWorker(array, index + 1);

            System.out.println(this + " has created: " + worker);
            new Thread(worker).start();

            threadList.add(this);
            try {
                synchronized (this) {
                    System.out.println(this + " is now waiting");
                    this.wait();
                    System.out.println(this + " got notified");
                }
            } catch (InterruptedException ex) {
                System.out.println("Error while waiting for termination");
                threadList.remove(this);
            }
        } else {
            threadList.forEach(worker -> {
                synchronized(worker) {
                    System.out.println(this + " notifying: " + worker);
                    worker.notify();
                }
            });
        }
    }

    @Override
    public String toString() {
        return "WorkerThread[" + index + "]";
    }  
}
示例输出

MainThread creating first WorkerThread and awaiting termination of last WorkerThread
WorkerThread[0] has been created
WorkerThread[0] run()
WorkerThread[1] has been created
WorkerThread[0] has created: WorkerThread[1]
WorkerThread[1] run()
WorkerThread[0] is now waiting
WorkerThread[2] has been created
WorkerThread[1] has created: WorkerThread[2]
WorkerThread[1] is now waiting
WorkerThread[2] run()
WorkerThread[3] has been created
WorkerThread[2] has created: WorkerThread[3]
WorkerThread[2] is now waiting
WorkerThread[3] run()
WorkerThread[3] notifying: WorkerThread[2]
WorkerThread[3] notifying: WorkerThread[1]
WorkerThread[2] got notified
WorkerThread[3] notifying: WorkerThread[0]
WorkerThread[1] got notified
WorkerThread[0] got notified
Last WorkerThread finished
Results:
10 25 73 7 

我的主要意见是,如果您对Java完全陌生,那么这就不是一个值得关注的问题!我完全同意,但我得到了几行代码,展示了如何创建一个线程,让它打印一个单词,这已经足够被一些人考虑了--如果我能想出如何让线程的run()写入数组并唤醒其他线程,我想我能想出其余的。notifyAll()方法通知所有等待的线程。我想你需要学习wait(),notifyAll(),你是怎么得到这个的?是作业吗?你应该向助教或教授寻求帮助,他们需要知道学生在作业中遇到问题的时间。如何将每个线程的值写入resultArray?谢谢。正是我需要的。如果你不介意的话,我有几个问题:1。真的需要@Override吗?(这实际上不会在任何地方“使用”,只是一项任务)2。“决赛”对这个项目有什么影响?3.有没有办法让结果打印在最后总是显示出来?如果我运行它几次,它打印结果的速度非常快,以至于线程开始消息都在末尾:D@GytisK1.它不是真正需要的,只是一种好的编码风格,可以帮助编译器帮助您防止错误。2.同样,这是一个很好的实践,可以帮助您最大限度地减少产生bug的机会。3.是的,有,你必须使用一个没有线程列表的记录器(它将保留调试消息的顺序并以fifo打印),我如何用最后一个线程通知所有线程?我(在main中)使用了else{synchronized(this){this.notifyAll()}},并且在完成后不会停止程序。
public static void main(String[] args) {
    final int[] myArray = new int[10];

    System.out.println("MainThread creating first WorkerThread and awaiting termination of last WorkerThread");
    Thread t = new Thread(new ArrayWorker(myArray, 0));
    try {
        t.start();
        t.join();
    } catch (InterruptedException ex) {
        ex.printStackTrace();
        System.exit(-1);
    }

    System.out.println("Last WorkerThread finished");
    for (int i : myArray) {
        System.out.print(i + " ");
    }
    System.out.println();
}
MainThread creating first WorkerThread and awaiting termination of last WorkerThread
WorkerThread[0] has been created
WorkerThread[0] run()
WorkerThread[1] has been created
WorkerThread[0] has created: WorkerThread[1]
WorkerThread[1] run()
WorkerThread[0] is now waiting
WorkerThread[2] has been created
WorkerThread[1] has created: WorkerThread[2]
WorkerThread[1] is now waiting
WorkerThread[2] run()
WorkerThread[3] has been created
WorkerThread[2] has created: WorkerThread[3]
WorkerThread[2] is now waiting
WorkerThread[3] run()
WorkerThread[3] notifying: WorkerThread[2]
WorkerThread[3] notifying: WorkerThread[1]
WorkerThread[2] got notified
WorkerThread[3] notifying: WorkerThread[0]
WorkerThread[1] got notified
WorkerThread[0] got notified
Last WorkerThread finished
Results:
10 25 73 7