【问题标题】:Producer-Consumer Problem with 2 processes in JavaJava中2个进程的生产者-消费者问题
【发布时间】:2021-04-10 11:34:26
【问题描述】:

我从教授那里得到了与生产者-消费者问题相关的任务。 任务是实现Producer-Consumer流程,但是

  1. 第一个进程应每次将其值增加 5
  2. 第二个进程应每次将其值除以 2。 我找到了一些代码示例,但没有关于多个进程的内容。这是其中之一。

--

// 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); 
                } 
            } 
        } 
    } 
} 

输出:

Producer produced-0
Producer produced-1
Consumer consumed-0
Consumer consumed-1
Producer produced-2

我不确定我是否完全理解我的任务。是否可以在此代码上实现给定的任务?我希望您给我一些建议或任何来源,以便我了解该怎么做。 提前谢谢大家!

【问题讨论】:

    标签: java operating-system


    【解决方案1】:

    如果我从您的描述中正确理解了您的问题,您应该从两个线程中更改该字段的值。您想创建一个类级字段和两个同步方法来更改他的值,然后创建两个线程并从它们交替调用这两个操作。我想你可以这样写:

        public class Solution0 {
    
        private volatile int count;
    
        public synchronized void increment() throws InterruptedException {
            count = count + 5;
            Thread.sleep(1000);
            notifyAll();
            wait();
        }
    
        public synchronized void divide() throws InterruptedException {
            count = count / 2;
            Thread.sleep(1000);
            notifyAll();
            wait();
        }
    
        public static void main(String[] args) {
            Solution0 solution = new Solution0();
            Thread t1 = new Thread(() -> {
                try {
                    while (true){
                        solution.increment();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            Thread t2 = new Thread(() -> {
                try {
                    while (true){
                        solution.divide();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t1.start();
            t2.start();
        }
    }
    

    此外,如果您需要实数,您可以将字段类型 countint 更改为 double

    【讨论】:

    • 哦,非常感谢!我认为这个是正确的
    • 对不起,我应该单独运行这个文件还是实现第一个代码?
    • 本示例有一个“main”方法,因此您可以从中创建 jar 存档并通过“java -example.jar”运行它
    • 对于白痴问题我很抱歉,但是可以在在线编译器中运行它吗?我尝试创建 jar 存档,但出现“没有主要清单属性”等错误:c
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多