本帖最后由 0xFFFF00 于 2017-11-24 16:32 编辑
作业题是要求用LinkedList 和 LinkedBlockingQueue分别写一个多线程生产者和消费者问题。我大致写好了,只是在运行的时候输出的东西有问题。现贴出代码,还请大佬指条路子。 下面首先放出作业要求里面的输出:
The following is an example output for 1 producer and 4 consumers, using the
LinkedBlockingQueue<T> implementation:
"Producer 1": 100 events produced
"Producer 1": 200 events produced
"Producer 1": 300 events produced
"Producer 1": 400 events produced
"Consumer 1": 100 events consumed
"Consumer 2": 100 events consumed
"Producer 1": 500 events produced
"Consumer 4": 100 events consumed
"Consumer 3": 100 events consumed
"Producer 1": 600 events produced
"Producer 1": 700 events produced
"Producer 1": 800 events produced
"Consumer 1": 200 events consumed
"Consumer 3": 200 events consumed
"Producer 1": 900 events produced
"Consumer 4": 200 events consumed
"Consumer 2": 200 events consumed
"Producer 1": 1000 events produced
Summary:
"Producer 1" produces 1000 events.
"Consumer 1" consumes 252 events.
"Consumer 2" consumes 246 events.
"Consumer 3" consumes 255 events.
"Consumer 4" consumes 247 events.
上面的是LInkedBlockingQueue的输出,下面的是LinkedList的输出:
The following is an example output for 1 producer and 4 consumers, using the
LinkedBlockingQueue<T> implementation:
"Producer 1": 100 events produced
"Producer 1": 200 events produced
"Producer 1": 300 events produced
"Producer 1": 400 events produced
"Consumer 1": 100 events consumed
"Consumer 2": 100 events consumed
"Producer 1": 500 events produced
"Consumer 4": 100 events consumed
"Consumer 3": 100 events consumed
"Producer 1": 600 events produced
"Producer 1": 700 events produced
"Producer 1": 800 events produced
"Consumer 1": 200 events consumed
"Consumer 3": 200 events consumed
"Producer 1": 900 events produced
"Consumer 4": 200 events consumed
"Consumer 2": 200 events consumed
"Producer 1": 1000 events produced
Summary:
"Producer 1" produces 1000 events.
"Consumer 1" consumes 252 events.
"Consumer 2" consumes 246 events.
"Consumer 3" consumes 255 events.
"Consumer 4" consumes 247 events.
下面是我写的代码,把t3, t4, t5拿掉还好。加上之后就有问题:
下面这个是用LInkedList写的,还没有添加Producer2,3,4 和 Consumer2,3,4.
- //LinkedList
- //Main.java
- import java.util.LinkedList;
- public class Main {
- public static LinkedList<String> ll = new LinkedList<String>();
-
- public static void main(String[] args) {
-
- Consumer consumer = new Consumer();
- Producer p = new Producer();
-
- try {
- Thread t1 = new Thread(consumer);
- Thread t2 = new Thread(p);
-
- t1.start(); // run t1
- t2.start(); // run t2
-
- t1.join(); // wait until t1 completes
- t2.join(); // wait until t2 completes
-
- System.out.println("\nFinished producing...\n");
- System.out.println("Summary:");
- System.out.println("'Producer 1' produces " + p.c + " events.\n");
- System.out.println("Finished consuming... ");
- System.out.println("'Consumer 1' consumes " + consumer.c + " events.");
-
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- //Producer.java
- import java.util.Random;
- public class Producer implements Runnable{
- public int c = 0;
- Random r = new Random();
-
- public void run() {
- System.out.println("Start producing... ");
- for (int i =0; i < 1000; i++) {
- synchronized (Main.ll){
- try {
- double nextDouble = r.nextDouble();
- Main.ll.add(Double.toString(nextDouble));
- c++;
- if (c % 100 == 0) {
- System.out.println("Producing progress: " + c);
- }
- }catch(Exception e) {
-
- }
- }
- }
- }
- }
- .//Consumer.java
- import java.util.Random;
- public class Consumer implements Runnable{
-
- public int c = 0;
-
- public void run(){
-
- Random r = new Random();
- System.out.println("Start consuming...");
-
- while (Main.ll.size() != 0 || c != 1000) {
- synchronized(Main.ll) {
- try {
- Main.ll.remove();
- Thread.sleep(r.nextInt(10));
- c++;
- if (c % 100 == 0) {
- System.out.println("Consume progress: " + c);
- }
- }catch(Exception e) {
-
- }
- }
- }
- }
-
- }
复制代码
@shiting @学联计算机培训