两个线程共享同一数组java

时间:2019-01-02 21:02:40

标签: java multithreading

我有两个线程,A和B。A必须创建消息并将其存储在内存中的列表中,而B必须从内存中的同一列表中读取第一条消息,将其从列表中删除并对其进行处理

A和B从主线程开始。

我的问题是如何创建一个由两个不同线程共享的列表?

2 个答案:

答案 0 :(得分:1)

您应该阅读以下内容:https://www.geeksforgeeks.org/producer-consumer-solution-using-threads-java/

他们那里的Java示例应该有助于您的方案进行一些修改。

// Java program to implement solution of producer 
// consumer problem. 
import java.util.LinkedList; 

public class Threadexample 
{ 
    public static void main(String[] args) 
                        throws InterruptedException 
    { 
        // Object of a class that has both produce() 
        // and consume() methods 
        final PC pc = new PC(); 

        // Create producer thread 
        Thread t1 = new Thread(new Runnable() 
        { 
            @Override
            public void run() 
            { 
                try
                { 
                    pc.produce(); 
                } 
                catch(InterruptedException e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
        }); 

        // Create consumer thread 
        Thread t2 = new Thread(new Runnable() 
        { 
            @Override
            public void run() 
            { 
                try
                { 
                    pc.consume(); 
                } 
                catch(InterruptedException e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
        }); 

        // Start both threads 
        t1.start(); 
        t2.start(); 

        // t1 finishes before t2 
        t1.join(); 
        t2.join(); 
    } 

    // This class has a list, producer (adds items to list 
    // and consumber (removes items). 
    public static class PC 
    { 
        // Create a list shared by producer and consumer 
        // Size of list is 2. 
        LinkedList<Integer> list = new LinkedList<>(); 
        int capacity = 2; 

        // Function called by producer thread 
        public void produce() throws InterruptedException 
        { 
            int value = 0; 
            while (true) 
            { 
                synchronized (this) 
                { 
                    // producer thread waits while list 
                    // is full 
                    while (list.size()==capacity) 
                        wait(); 

                    System.out.println("Producer produced-"
                                                  + value); 

                    // to insert the jobs in the list 
                    list.add(value++); 

                    // notifies the consumer thread that 
                    // now it can start consuming 
                    notify(); 

                    // makes the working of program easier 
                    // to  understand 
                    Thread.sleep(1000); 
                } 
            } 
        } 

        // Function called by consumer thread 
        public void consume() throws InterruptedException 
        { 
            while (true) 
            { 
                synchronized (this) 
                { 
                    // consumer thread waits while list 
                    // is empty 
                    while (list.size()==0) 
                        wait(); 

                    //to retrive the ifrst job in the list 
                    int val = list.removeFirst(); 

                    System.out.println("Consumer consumed-"
                                                    + val); 

                    // Wake up producer thread 
                    notify(); 

                    // and sleep 
                    Thread.sleep(1000); 
                } 
            } 
        } 
    } 
} 

正如其他人所建议的,我认为您应该花一些时间来了解线程和生产者消费者模式。

答案 1 :(得分:1)

首先创建列表的实例。

第二次将列表作为参数传递给两个可运行对象的构造函数。

public static void main(String args[]){
    List<MyType> myList = new ArrayList<>();
    Runnable r1 = new MyRunnable(myList);
    Runnable r2 = new MyRunnable(myList);
    new Thread(r1).start();
    new Thread(r2).start();
}

public class MyRunnable implements Runnable {
    List list;

    public MyRunnable(List list) {
        this.list = list;
    }

    @Override
    public void run() {}
}