Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 线程、同步、填充和删除int';从数组中删除_Java_Arrays_Multithreading_Synchronization - Fatal编程技术网

Java 线程、同步、填充和删除int';从数组中删除

Java 线程、同步、填充和删除int';从数组中删除,java,arrays,multithreading,synchronization,Java,Arrays,Multithreading,Synchronization,我需要写一个程序,它有一个整数数组。假设数组的长度为3。首先,它是空的。现在,程序必须使用两个线程,一个将整数写入空数组,另一个从中删除整数。我给大家举个例子: thread1 put in value 1 //array has value: 1 thread1 put in value 2 //array has two values: 1 and 2 thread2 deleted value 2 //array now has only one value: 1 thread1 put

我需要写一个程序,它有一个整数数组。假设数组的长度为3。首先,它是空的。现在,程序必须使用两个线程,一个将整数写入空数组,另一个从中删除整数。我给大家举个例子:

thread1 put in value 1 //array has value: 1
thread1 put in value 2 //array has two values: 1 and 2
thread2 deleted value 2 //array now has only one value: 1
thread1 put in value 3 //array has two values:  1 and 3
thread1 put in value 4 //array has three values: 1,3,4
thread1 wants to put value 5 into the array, but it has to wait, because the array is full
thread2 deleted value 4// array has 2 values: 1, 3 and so on........
我的老师说,通过使用指定的计数器实现for循环(例如,每个线程必须经过10个循环)来完成所有这些任务是最好的也是最简单的。现在我已经编写了一些代码,但我无法理解如何实现同步对象(整数数组),以便两个线程都可以使用它。代码如下:

public class Antras {

public static void main(String[] args) {
    System.out.println("Program starts working");
    begin();
    System.out.println("Program ends work");
}

public static void begin() {
    synchronizationObject channel = new synchronizationObject();
    try {

        Thread putIn = new ReadThread(channel);
        putIn.start();

        Thread takeOut = new WriteThread(channel);
        takeOut.start();

        putIn.join();
        takeOut.join();

        System.out.println("main() ended work");
    } catch (InterruptedException exc) {
        System.out.println("Error " + exc);
    }
}
}

class ReadThread extends Thread {

private synchronizationObject channel;

public ReadThread(synchronizationObject channel) {
    this.channel = channel;
}

public void run() {
    System.out.println("Thread " + this + "working");


    channel.working = false;
    System.out.println("Thread " + this + "ends work");
}
}



class WriteThread extends Thread {

private synchronizationObject channel;

public WriteThread(synchronizationObject channel) {
    this.channel = channel;
}

public void run() {
    System.out.println("Thread " + this + "working");


    System.out.println("Thread " + this + "end work");
}
}




class synchronizationObject {

public static int N = 3; 
int[] arrayOfInts = new int[N];

synchronized void takeOut() {

}

synchronized void putIn(int d) {

}
}
这是WriteThread的运行方法:

 public void run() {
    System.out.println("WriteThread" + this + "started");
    for(int i = 0; i<10; i++){
        channel.putIn(i);   //puts in a value
    }
    System.out.println("WriteThread" + this + "ended");
}
public void run(){
System.out.println(“WriteThread”+此+“已启动”);

对于(int i=0;i您在
WriteThread
/
ReadThread
/
synchronizationObject
)中似乎没有其他模板;到目前为止您尝试了什么?您可以从以下内容开始:Re,“长度为3的整数数组…。首先,它是空的”。不存在空数组。听起来您试图描述更高级别的数据结构(例如,
集合
列表
)在实现中使用数组。我认为您需要更好地了解更高级别对象的行为,编写一些测试,创建通过测试的对象实现,…在您担心线程安全或编写任何创建新线程的代码之前,先做所有这些ld在你的案例中比数组更合适仔细想想,你的老师有没有告诉你实现你自己的集合、列表或队列类?Java有所有这些类的实现,你可以不用编写任何代码就可以使用。不要重新发明轮子,除非作业的目的是专门教你轮子是如何发明的。
 public void run() {
    System.out.println("WriteThread" + this + "started");
    for(int i = 0; i<10; i++){
        channel.putIn(i);   //puts in a value
    }
    System.out.println("WriteThread" + this + "ended");
}