您的位置:首页 > 其它

多线程实验_多生产者多消费者操作一个栈list

2016-06-05 20:35 465 查看
//只是很简单的实现了数据集,数据集的操作服务类,多线程模拟多生产者和多消费者。最后一个测试类。

数据集类

package entity;

import java.util.ArrayList;
import java.util.List;

/**
* synchronized同步对象是MyStack实例对象
* list只是一个元素
* @author cindy
*
*/
public class MyStack {

private List list = new ArrayList<String>();

/**
* 生产栈元素
*/
synchronized public void push() {
try {
while (list.size() == 1) {
System.out.println("push操作阻塞" + ",线程是" + Thread.currentThread().getName());
this.wait();
}
list.add("hello vincent" + Math.random());
this.notifyAll();
System.out.println(Thread.currentThread().getName() + "进程进行了一次push操作" + "push=" + list.size());
} catch (Exception e) {
// TODO: handle exception
}
}

/**
* 消耗栈元素
*
* @return
*/
synchronized public String pop() {

String result = "";
try {

while (list.size() == 0) {
System.out.println("pop操作阻塞:" + Thread.currentThread().getName() + "线程呈wait状态");
this.wait();
}
result = list.get(0) + "";
list.remove(0);
this.notifyAll();
System.out.println("pop=" + list.size() + "\t" + Thread.currentThread().getName());
} catch (Exception e) {
// TODO: handle exception
}
return result;

}

public MyStack() {
// TODO Auto-generated constructor stub
}

}


生产者服务类

package service;

import entity.MyStack;

public class P {

private MyStack myStack;

public P(MyStack myStack) {
// TODO Auto-generated constructor stub
this.myStack = myStack;
}

public void pushService() {
myStack.push();

}
}


消费者服务类

package service;

import entity.MyStack;

public class C {
private MyStack myStack;

public C(MyStack myStack) {
// TODO Auto-generated constructor stub
this.myStack = myStack;
}

public void popService() {
System.out.println("pop=" + myStack.pop());
}
}


生产者线程类

package extthread;

import service.P;

public class P_thread extends Thread {

private P p;

public P_thread(P p) {
this.p = p;
}

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while (true) {
p.pushService();

}
}
}


消费者线程类

package extthread;

import service.C;

public class C_thread extends Thread {

private C c;

public C_thread(C c) {
this.c = c;
}

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while (true) {
c.popService();
}
}

}


测试类

package test.run;

import entity.MyStack;
import extthread.C_thread;
import extthread.P_thread;
import service.C;
import service.P;

public class Run {

public Run() {
// TODO Auto-generated constructor stub
}

public static void main(String[] args) {

MyStack myStack = new MyStack();
C c1 = new C(myStack);
C c2 = new C(myStack);
C c3 = new C(myStack);
P p1 = new P(myStack);
P p2 = new P(myStack);
P p3 = new P(myStack);
P_thread p_thread1 = new P_thread(p1);
P_thread p_thread2 = new P_thread(p2);
P_thread p_thread3 = new P_thread(p3);

C_thread c_thread1 = new C_thread(c1);
C_thread c_thread2 = new C_thread(c2);
C_thread c_thread3 = new C_thread(c3);
p_thread1.start();
p_thread2.start();
p_thread3.start();
c_thread1.start();
c_thread2.start();
c_thread3.start();

}

}


测试结果图

从这幅图可以看到多个生产者线程往list列表生成数据,消费者消费生产者生成的数据。同时可以看到list长度一直保持在1。当一个生产者线程产生了一个数据后,其他生产者线程将阻塞,等待消费者线程消费完后,在由其中一个生产者阻塞线程生成下一个数据。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: