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");
    }
}
3
  • Can you be more specific about your requirement. You want to use array instead of arraylist ? Commented Aug 22, 2022 at 6:18
  • google.com/… Commented Aug 22, 2022 at 6: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. Commented Aug 22, 2022 at 7:28

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.