Java 使用ArrayBlockingQueue和mutex的多线程代码中的疑问

Java 使用ArrayBlockingQueue和mutex的多线程代码中的疑问,java,multithreading,mutex,blockingqueue,Java,Multithreading,Mutex,Blockingqueue,我正在尝试编写一个多线程代码。但说真的,我不明白从哪里开始。我的头也在砰砰作响。请帮帮我 我的任务是, 有一个长度为1的队列,称为pending_tasks,其中包含需要某些处理的任务 还有另一个长度为1的队列,称为已完成的\u任务,其中包含完成处理并准备交付的任务 我的实施思路, 首先创建两个阻塞队列,挂起的\u任务和已完成的\u任务 一个线程(生产者)总是监听来自外部的任务,如果被放入挂起的任务中 一个线程(使用者)始终准备从挂起的\u任务接收任务并开始处理,然后放入已完成的\u任务 然后再

我正在尝试编写一个多线程代码。但说真的,我不明白从哪里开始。我的头也在砰砰作响。请帮帮我

我的任务是,

  • 有一个长度为1的队列,称为
    pending_tasks
    ,其中包含需要某些处理的任务
  • 还有另一个长度为1的队列,称为
    已完成的\u任务
    ,其中包含完成处理并准备交付的任务
  • 我的实施思路,

  • 首先创建两个阻塞队列,
    挂起的\u任务
    已完成的\u任务
  • 一个线程(生产者)总是监听来自外部的任务,如果被放入
    挂起的任务中
  • 一个线程(使用者)始终准备从
    挂起的\u任务
    接收任务并开始处理,然后放入
    已完成的\u任务
  • 然后再次进入
    挂起的\u任务
    ,每当有任务出现时,开始相同的处理
  • 基本上,这是一个单一生产者单一消费者的问题
  • 我的困惑,

    我知道可以通过使用ArrayBlockingQueue和互斥来编写代码。但我不明白我该怎么开始。我对互斥体有很好的理解,我从这里读到了互斥体,也很好地理解了blockingQueue,因为我在这个网站上读到了很多问题

    你能给我一些实现指南吗,这样我就可以编写这个多线程代码了

    我已经为此编写了一些代码,但这并不是我任务的最终目标

    提前谢谢。期待您的友好回复

    编辑编号1

    请看我下面的代码。这段代码工作正常,但这段代码缺少一项功能。请帮我补充一下,给我一些指导

    功能是,

  • 当生产者线程在挂起的任务队列中放入一些值时,它会在那里等待一段时间。如果在这段时间内消费者将结果提供给消费者,那么就可以了。否则,它会显示超时,producer会获取另一个值,并在pending_任务队列中获取pput,然后启动相同的进程
  • 请帮助我添加上述功能。我认为我们必须在生产者线程和消费者线程之间进行通信,线程通信是通过使用互斥来完成的(我认为)。请帮助我实现相同的功能

    我的代码,

    多线程类

    package multithread;
    
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.BlockingQueue;
    
    public class MultiThread {
    
        public static BlockingQueue<Integer> pending_task;
        public static BlockingQueue<Integer> completed_task;
    
        public MultiThread(int length) {
            pending_task = new ArrayBlockingQueue<Integer>(length, true);
            completed_task = new ArrayBlockingQueue<Integer>(length, true);
        }
    }
    
    package multithread;
    
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Producer implements Runnable {
    
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                try {
                    System.out.println("PRODUCER: Try to put value  " + i + "  in the pending queue");
                    MultiThread.pending_task.put(i);
                    System.out.println("PRODUCER: Successfully put value  " + i + "  in the pending queue, now its turn to consumer");
                } catch (InterruptedException ex) {
                    Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    package multithread;
    
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Consumer implements Runnable {
    
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                try {
                    System.out.println("CONSUMER: Try to take value from the pending queue");
                    int val = MultiThread.pending_task.take();
                    System.out.println("CONSUMER:  Successfully take value, and that is   " + val);
                    System.out.println("CONSUMER: Processing starts");
                    Thread.sleep(1000);
                    System.out.println("CONSUMER: Processing ends");
                    System.out.println("CONSUMER: Try to put that  that value in  completed queue, and the value is   " + val);
                    MultiThread.completed_task.put(val);
                    System.out.println("CONSUMER: Successfully put into completed queue");
    
                    //Serve this value to the corresponding user
                } catch (InterruptedException ex) {
                    Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
                }
    
            }
        }
    }
    
    package multithread;
    
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class DeliveryBoy implements Runnable {
    
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                try {
                    System.out.println("DELIVERYBOY: Waiting for the value near completed queue");
                    int val = MultiThread.completed_task.take();
                    System.out.println("DELIVERYBOY:  Succesfully take value from completed queue and the vlue is  " + val);
                    //Serve this value to the corresponding user
                } catch (InterruptedException ex) {
                    Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
                }
    
            }
        }
    }
    
    package multithread;
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO code application logic here
            MultiThread ml = new MultiThread(1);
            new Thread(new Producer()).start();
            new Thread(new Consumer()).start();
            new Thread(new DeliveryBoy()).start();
        }
    }
    

    公众作废认沽权证(E) 抛出中断异常

    在该队列的尾部插入指定的元素,等待 **队列已满时可用的空间

    公共电子商务 抛出中断异常

    从接口复制的说明:BlockingQueue检索并删除 此队列的头,在必要时等待,直到元素变为 可用

    因此,您只需从线程中调用这些方法。

    试试这个(研究javadoc),当你有更具体的问题时,你可以再问一次。

    你的问题很可能会被解决,除非你发布你当前的代码,并围绕为什么不起作用提出一个问题。目前,你的问题非常模糊和开放,只能由为你写整个事情的人来回答。你的互斥链接相当旧。虽然您仍然可以使用wait/notify for consumer/producer模式,但还有更有用、更高级的解决方案。我建议你读一下。@DuncanJones我添加了有问题的代码。请看that@DuncanJones请看我的代码,并给出一些指导。请看编辑1,它显示了我的代码和一些功能,我想添加。请推荐一些专家建议。我该怎么做呢。如果可能的话,请提供一些实施指南。你在回答中解释的东西我已经知道了。我觉得我在理论方面已经很好了。但我无法将其转换为代码。我做了一些代码(请参阅问题的更新部分),但有很多问题,请帮助我解决这些问题。是的,我想在其中添加互斥概念。这是一个问题,我如何实现这个,thia中缺少一些东西,请指导我,我如何在这里实现线程,以及互斥。@devsda:检查这个:根据这个页面,我必须为prodcer和consumer创建不同的类?但实际上我希望所有这些都在同一个类的同一个模块中执行。我可以这样做吗?请看编辑1,它显示了我的代码和一些我想添加的功能。请推荐一些专家建议。我该怎么做呢。如果可能,请提供一些实施指南。