生产者-消费者问题

mkshixfv  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(457)

我从教授那里得到了一个关于生产者和消费者问题的任务。任务是实现生产者-消费者过程,但是
第一个进程每次应增加其值5
第二个进程每次应将其值除以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

我不确定我是否完全理解我的任务。有可能在这个代码上实现给定的任务吗?我想你给我一些建议或任何来源,这样我就可以明白该怎么做。提前谢谢大家!

hfsqlsce

hfsqlsce1#

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

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

此外,还可以更改字段的类型 countintdouble ,如果你需要一个实数。

相关问题