62,615
社区成员
发帖
与我相关
我的任务
分享
class Product {
static int count=0 ;
private int id = 0;
public synchronized static Product nextProduct(){
Product p = new Product();
count++;
p.id = count;
return p;
}
private Product() {
super();
}
public String toString() {
return id + " 号产品";
}
}
public class ProducerConsumer {
public static void main(String [] args) {
SyncStack ss = new SyncStack();
Producer pd1 = new Producer(ss,"生产者1");
Producer pd2 = new Producer(ss,"生产者2");
Producer pd3 = new Producer(ss,"生产者3");
Consumer cs1 = new Consumer(ss,"消费者1");
Consumer cs2 = new Consumer(ss,"消费者2");
Consumer cs3 = new Consumer(ss,"消费者3");
Thread p1 = new Thread(pd1);
Thread p2 = new Thread(pd2);
Thread p3 = new Thread(pd3);
Thread c1 = new Thread(cs1);
Thread c2 = new Thread(cs2);
Thread c3 = new Thread(cs3);
p1.start();c1.start();
p2.start();c2.start();
p3.start();c3.start();
while(true) {
if((c1.isAlive()||c2.isAlive()||c3.isAlive()) == false) {
System.out.println("操作结束,一共生产了" +Product.count + "件产品!");
return ;
}
}
}
}
class Product {
static int count=0 ;
private int id = 0;
Product() {
id = count+1;
count++;
}
public String toString() {
return id + " 号产品";
}
}
class SyncStack {
private Product [] arr = new Product[10];
private int index = 0;
public synchronized void push(Product pd ,String name) {
while(index == arr.length) {
try {
this.wait();
}catch(InterruptedException x) {
x.printStackTrace();
}
}
arr[index] = pd;
System.out.println(name + "放进" + pd + "!");
index++;
this.notifyAll();
}
public synchronized Product pop(String name) {
while(index == 0) {
try {
this.wait();
}catch(InterruptedException x) {
x.printStackTrace();
}
}
index--;
this.notifyAll();
System.out.println(name + "拿走" + arr[index] +"!");
return arr[index];
}
}
class Producer implements Runnable {
private SyncStack ss;
private String name ;
Producer(SyncStack ss,String s) {
this.ss = ss;
name = s;
}
public void run() {
for(int i = 1;i<21;i++) {
Product pd = new Product();
ss.push(pd,name);
try {
Thread.sleep((int)(Math.random()*50));
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
private SyncStack ss;
private String name;
Consumer(SyncStack ss,String s) {
this.ss = ss;
name = s;
}
public void run() {
for(int i = 0;i<20;i++) {
Product pd = ss.pop(name);
try {
Thread.sleep((int)(Math.random()*50));
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
//ls正解
//这里的try要放到for (int i = 1; i < 21; i++)里面去吗
class Producer implements Runnable {
private SyncStack ss;
Producer(SyncStack ss) {
this.ss = ss;
}
public void run() {
for (int i = 1; i < 21; i++) {
Product pd = new Product(i);
ss.push(pd);
}
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ProducerConsumer {
public static void main(String [] args) {
SyncStack ss = new SyncStack();
Producer pd1 = new Producer(ss,"生产者1");
Producer pd2 = new Producer(ss,"生产者2");
Producer pd3 = new Producer(ss,"生产者3");
Consumer cs1 = new Consumer(ss,"消费者1");
Consumer cs2 = new Consumer(ss,"消费者2");
Consumer cs3 = new Consumer(ss,"消费者3");
Thread p1 = new Thread(pd1);
Thread p2 = new Thread(pd2);
Thread p3 = new Thread(pd3);
Thread c1 = new Thread(cs1);
Thread c2 = new Thread(cs2);
Thread c3 = new Thread(cs3);
p1.start();c1.start();
p2.start();c2.start();
p3.start();c3.start();
while(true) {
if((c1.isAlive()||c2.isAlive()||c3.isAlive()) == false) {
System.out.println("操作结束,一共成产了" +Product.count + "件产品!");
return ;
}
}
}
}
class Product {
static int count=0 ;
private int id = 0;
Product() {
id = count+1;
count++;
}
public String toString() {
return id + " 号产品";
}
}
class SyncStack {
private Product [] arr = new Product[10];
private int index = 0;
public synchronized void push(Product pd) {
while(index == arr.length) {
try {
this.wait();
}catch(InterruptedException x) {
x.printStackTrace();
}
}
arr[index] = pd;
index++;
this.notifyAll();
}
public synchronized Product pop() {
while(index == 0) {
try {
this.wait();
}catch(InterruptedException x) {
x.printStackTrace();
}
}
index--;
this.notifyAll();
return arr[index];
}
}
class Producer implements Runnable {
private SyncStack ss;
private String name ;
Producer(SyncStack ss,String s) {
this.ss = ss;
name = s;
}
public void run() {
for(int i = 1;i<21;i++) {
Product pd = new Product();
ss.push(pd);
System.out.println(name + "放进" + pd + "!");
try {
Thread.sleep((int)(Math.random()*50));
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
private SyncStack ss;
private String name;
Consumer(SyncStack ss,String s) {
this.ss = ss;
name = s;
}
public void run() {
for(int i = 0;i<20;i++) {
Product pd = ss.pop();
System.out.println(name + "拿走" + pd +"!");
try {
Thread.sleep((int)(Math.random()*50));
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ProducerConsumer {
public class ProducerConsumer {
public static void main(String[] args) {
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
Thread p1 = new Thread(p);
Thread c1 = new Thread(c);
p1.start();
c1.start();
while (c1.isAlive() == true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
}
System.out.println("操作结束,一共进行了" + SyncStack.num + "次操作!");
}
}
class Product {
private int id;
Product(int id) {
this.id = id;
}
public String toString() {
return "Product:" + id;
}
}
class SyncStack {
private Product[] arr = new Product[10];
private int index = 0;
static int num = 0;
public synchronized void push(Product pd) {
while (index == arr.length) {
try {
this.wait();
} catch (InterruptedException x) {
x.printStackTrace();
}
}
num++;
arr[index] = pd;
index++;
System.out.println("生产了" + pd + "!");
this.notify();
}
public synchronized Product pop() {
while (index == 0) {
try {
this.wait();
} catch (InterruptedException x) {
x.printStackTrace();
}
}
num++;
index--;
Product pd = arr[index];
System.out.println("消费了" + pd + "!");
this.notify();
return pd;
}
}
class Producer implements Runnable {
private SyncStack ss;
Producer(SyncStack ss) {
this.ss = ss;
}
public void run() {
for (int i = 1; i < 21; i++) {
Product pd = new Product(i);
ss.push(pd);
}
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Consumer implements Runnable {
private SyncStack ss;
Consumer(SyncStack ss) {
this.ss = ss;
}
public void run() {
for (int i = 0; i < 20; i++) {
Product pd = ss.pop();
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}