0

I should create Thread as ProducerThread and ConsumerThread those execute sequentially and put elements to queue. In this implementation I used ArrayList.

How might I convert an ArrayList object to integer Array in java.

This is my ConsumerThread class.

import java.util.ArrayList;
public class ConsumerThread implements Runnable {
    ArrayList<Integer> queue = new ArrayList<Integer>();

public ConsumerThread(ArrayList<Integer> queue) {
    this.queue = queue;
}

@Override
public void run() {
    synchronized(queue) {
        int value;
        while(true) {
            try {
                queue.wait();
                System.out.println("Consumer Started");
                value = queue.remove(0);
                System.out.println("Consumer thread consumes " + value);
                System.out.println("Elements in Queue = " + queue);
                Thread.sleep(1000);
                queue.notify();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    }

}

This is my ProducerThread class.

import java.util.ArrayList;

public class ProducerThread implements Runnable {
    ArrayList<Integer> queue = new ArrayList<Integer>();
    
    public ProducerThread(ArrayList<Integer> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        synchronized(queue) {
            int value = 10;
            while(true) {
                try {
                    queue.notify();
                    System.out.println("Producer Started");
                    System.out.println("Producer adding value = " + value + " to Queue");
                    queue.add(value);
                    value = value + 10;
                    //Thread.sleep(1000);
                    queue.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

This is my main class.

import java.util.ArrayList;

public class ThreadTest {
    public static void main(String[] args) {
        ArrayList<Integer> queue = new ArrayList<Integer>();
        Thread producer = new Thread(new ProducerThread(queue));
        Thread consumer = new Thread(new ConsumerThread(queue));
        
        producer.start();
        consumer.start();
        System.out.println("Starting");
    }
}
  • Can you be more specific about your requirement. You want to use array instead of arraylist ? – Ashishkumar Singh Aug 22 '22 at 06:18
  • https://www.google.com/search?q=list+convert+to+array+java&oq=list+convert+to+array+&aqs=chrome.2.69i57j0i512l2j0i30j0i8i30j0i8i15i30j0i5i30j69i60.7300j0j1&sourceid=chrome&ie=UTF-8 – 时间只会一直走 Aug 22 '22 at 06:46
  • By the way, your Consumer and Producer classes need new names. They are not threads, so putting "Thread" in their name is confusing. "Task" is one commonly used word for a `Runnable` or `Callable`. Also, I suggest learning about using the Executors framework and `ExecutorService` class to run your tasks rather than calling `new Thread`. – Basil Bourque Aug 22 '22 at 07:28

1 Answers1

0

toArray() method convert any ArrayList to Array Objects -> Object[]

ArrayList<Integer> queue = new ArrayList<>();
queue.add(23);
queue.add(6765);
Object[] arrayQueue = queue.toArray();
System.out.println("arrayList = " + queue + "\n arrayNative = ");
Arrays.stream(arrayQueue).forEach(System.out::println);
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Omar Hussien
  • 313
  • 1
  • 9