求大佬帮忙改一下程序
本帖最后由 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拿掉还好。加上之后就有问题:
//LinkedBlockingQueue
//Main.java
import java.util.concurrent.LinkedBlockingQueue;
public class Main {
public static LinkedBlockingQueue<String> s = new LinkedBlockingQueue<String>(100);
public static void main(String[] args) {
Consumer c = new Consumer();
Producer p = new Producer();
try {
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
//Thread t3 = new Thread(c);
//Thread t4 = new Thread(c);
//Thread t5 = new Thread(c);
t1.start(); // run t1
t2.start(); // run t2
//t3.start();
//t4.start();
//t5.start();
t1.join(); // wait until t1 completes
t2.join(); // wait until t2 completes
//t3.join();
//t4.join();
//t5.join();
System.out.println("\nFinished producing...");
System.out.println("Summary:");
System.out.println("'Producer 1' produces " + p.counter + " events.\n");
System.out.println("Finished consuming... ");
System.out.println("'Consumer 1' consumes " + c.counter + " events.");
//System.out.println("'Consumer 2' consumes " + c.counter + " events.");
//System.out.println("'Consumer 3' consumes " + c.counter + " events.");
//System.out.println("'Consumer 4' consumes " + c.counter + " events.");
}catch(Exception e){
e.printStackTrace();
}
}
}
//Producer.java
import java.util.Random;
public class Producer implements Runnable{
public int counter = 0;
public void run() {
Random n = new Random();
//Thread t = new Thread();
System.out.println("Start producing... ");
for(int i=0; i<1000; i++) {
try {
double nextDouble = n.nextDouble();
Main.s.put(Double.toString(nextDouble));
counter++;
//System.out.println(nextDouble);
if (counter % 100 == 0) {
System.out.println("Producer progress " + counter);
}
}catch(InterruptedException e) {
}
}
}
}
//Consumer.Java
import java.util.Random;
public class Consumer implements Runnable{
public int counter = 0;
public void run() {
Random n = new Random();
//Thread t = new Thread();
System.out.println("Start consuming... ");
for(int i=0; i<1000; i++) {
try {
Main.s.take();
Thread.sleep(n.nextInt(10));
counter++;
//System.out.println(Main.s);
if (counter % 100 == 0) {
System.out.println("Consumer progress "+ counter);
}
}catch(InterruptedException e) {
}
}
}
}
下面这个是用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 @学联计算机培训 danny 发表于 2017-11-25 09:27
楼主让我打开眼界了
您老也让我开眼界了,每个帖子连着水两次确实是一个赚积分的好办法 本人电脑水平有限帮不了楼主。希望有别的大神能在此帖里回帖帮到楼主。 这么专业的东西,我来开开眼界就好了。 表示看不懂的说 实现四化 发表于 2017-11-26 10:06
好久没来,抱抱。
你抱楼上干嘛:lol 一陣眼花撩亂,只好看看大家的回帖,從中間學習點東西:loveliness: 0xFFFF00 发表于 2017-11-26 10:35
你抱楼上干嘛
估计是有受虐倾向:D