java - 当列表达到一定数量时执行操作

标签 java list design-patterns

我有一个线程将连接添加到列表中。当列表大于两个元素时,我的应用程序需要选择 2 个随机元素。

之后,程序对连接执行一些操作,然后从列表中删除这两个连接。

我已经尝试运行一个单独的线程并检查列表是否大于 2 个元素。

当它变大时,它会从列表中随机选择 2 个元素并对它们执行操作,然后将它们从列表中删除。

有没有更好的方法以设计模式的方式做到这一点?我不认为运行另一个线程来不断检查列表是否大于 2 是一个好的解决方案。

最佳答案

您可以通过生产者消费者问题来实现您的解决方案,其中一个线程正在检查列表的大小,当大小达到 2 时,其他线程开始从列表中消费(删除)。线程的添加和删除都应该通过同步块(synchronized block)来执行,这样才不会出现差异。

您可以尝试以下方法来解决您的问题。

`

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 consumer (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); 
            } 
        } 
    } 
} 

} `

看这里 https://www.geeksforgeeks.org/producer-consumer-solution-using-threads-java/

关于java - 当列表达到一定数量时执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55985320/

相关文章:

c# - 如何在 C# 中列出 .zip 文件夹的内容?

python - 将包含值列表的字典转换为数据框

java - 什么时候对 Java 中的享元字符串有益?

java - 如果我可以在创建者类中拥有多个工厂方法,为什么我需要抽象工厂模式?

java - Hibernate 中按计数排序

java - 如何在Scenebuilder/JavaFX中的GridPane中设置所有网格的大小和各种属性

java - 嵌套 c :choose tags not working

C++ 列表类 vs2010 Ultimate

c# - 评估我的代码设计 - 简单片段

java - 以编程方式在 TestNG xml 套件中设置测试参数以实现并行执行