62,615
社区成员
发帖
与我相关
我的任务
分享
static ArrayQueue _queue = new ArrayQueue(10);
// static Object[] _queue = new Object[100];
static int index = 0;
@Test
public void test02() throws Exception {
// 使用 wait notify 实现一个队列,队列有2个方法,add 和 get
// 。add方法往队列中添加元素,get方法往队列中获得元素。队列必须是线程安全的。
// 如果get执行时,队列为空,线程必须阻塞等待,直到有队列有数据。如果add时,队列已经满,
// 则add线程要等待,直到队列有空闲空间。
//实现这么一个队列,并写一个测试代码,使他工作在多线程的环境下,证明,它的工作是正确的。给出程序和运行的截图。
Thread a1 = new Thread(new addThread(),"AddThread-1");
Thread a2 = new Thread(new addThread(),"AddThread-2");
Thread a3 = new Thread(new addThread(),"AddThread-3");
a1.start();
a2.start();
a3.start();
Thread g1 = new Thread(new getThread(), "GetThread-1");
Thread g2 = new Thread(new getThread(), "GetThread-2");
Thread g3 = new Thread(new getThread(), "GetThread-3");
g1.start();
g2.start();
g3.start();
a1.join();
a2.join();
a3.join();
g1.join();
g2.join();
g3.join();
}
static class ArrayQueue {
private Object[] _queue = new Object[10];
private int index = 0;
private int num = 0;
public ArrayQueue(int length) {
_queue = new Object[length];
}
public synchronized void add() {
_queue[index] = "Content-" + num++;
System.out.println("生产者:" + _queue[index]);
index++;
if (index > 0 && index < _queue.length) {
notify(); //唤醒消费
}
if (index == _queue.length) {
try {
wait(); //等待消费
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void get() {
Object rs = _queue[0]; //消费队列中第一个元素
System.out.println("消费者:" + rs);
Object[] temp = new Object[_queue.length];
System.arraycopy(_queue, 1, temp, 0, _queue.length - 1); //向前移动队列
_queue = temp;
index--;
if (index == 0) {
try {
wait(); // 等待生产
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (index == _queue.length - 1) {
notify(); //唤醒生产
}
}
}
class addThread implements Runnable {
// public addThread(String name) {
// super(name);
// }
@Override
public void run() {
while (true) {
_queue.add();
}
}
}
class getThread implements Runnable {
// public getThread(String name) {
// super(name);
// }
@Override
public void run() {
while (true) {
_queue.get();
}
}
}
public class App
{
static ArrayQueue _queue = new ArrayQueue(10);
// static Object[] _queue = new Object[100];
static int index = 0;
public static void main(String[] args) throws InterruptedException {
// 使用 wait notify 实现一个队列,队列有2个方法,add 和 get
// 。add方法往队列中添加元素,get方法往队列中获得元素。队列必须是线程安全的。
// 如果get执行时,队列为空,线程必须阻塞等待,直到有队列有数据。如果add时,队列已经满,
// 则add线程要等待,直到队列有空闲空间。
//实现这么一个队列,并写一个测试代码,使他工作在多线程的环境下,证明,它的工作是正确的。给出程序和运行的截图。
Thread a1 = new Thread(new addThread(),"AddThread-1");
Thread a2 = new Thread(new addThread(),"AddThread-2");
Thread a3 = new Thread(new addThread(),"AddThread-3");
a1.start();
a2.start();
a3.start();
Thread g1 = new Thread(new getThread(), "GetThread-1");
Thread g2 = new Thread(new getThread(), "GetThread-2");
Thread g3 = new Thread(new getThread(), "GetThread-3");
g1.start();
g2.start();
g3.start();
a1.join();
a2.join();
a3.join();
g1.join();
g2.join();
g3.join();
}
static class ArrayQueue {
private Object[] _queue = new Object[10];
private int index = 0;
private int num = 0;
public ArrayQueue(int length) {
_queue = new Object[length];
}
public synchronized void add() {
while (index == _queue.length) {
try {
wait(); //等待消费
} catch (InterruptedException e) {
e.printStackTrace();
}
}
_queue[index] = "Content-" + num++;
System.out.println("生产者:" + _queue[index]);
index++;
notifyAll(); //唤醒消费
}
public synchronized void get() {
while (index == 0) {
try {
wait(); // 等待生产
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Object rs = _queue[0]; //消费队列中第一个元素
System.out.println("消费者:" + rs);
Object[] temp = new Object[_queue.length];
System.arraycopy(_queue, 1, temp, 0, _queue.length - 1); //向前移动队列
_queue = temp;
index--;
//唤醒生产
notifyAll();
}
}
static class addThread implements Runnable {
// public addThread(String name) {
// super(name);
// }
@Override
public void run() {
while (true) {
_queue.add();
}
}
}
static class getThread implements Runnable {
// public getThread(String name) {
// super(name);
// }
@Override
public void run() {
while (true) {
_queue.get();
}
}
}
}
烦死,一大早就来看代码,你也试着看看