提交 fe9af858 编写于 作者: 武汉红喜's avatar 武汉红喜

concurrent test

上级 dc4da67b
package org.hongxi.java.util.concurrent.sync;
package org.hongxi.java.util.concurrent;
/**
* @author shenhongxi 2019/8/11
* @author shenhongxi 2019/8/13
*/
public class WorkStack {
public class BlockingStack<E> {
private static final int DEFAULT_CAPACITY = 10;
private int index = 0;
private Work[] works = new Work[6];
Object[] items;
int size;
public synchronized void push(Work work) {
while (index == works.length) {
BlockingStack() {
items = new Object[DEFAULT_CAPACITY];
}
BlockingStack(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException();
items = new Object[capacity];
}
public synchronized void push(E item) {
while (size == items.length) {
try {
this.wait();
} catch (InterruptedException e) {}
}
this.notifyAll();
works[index++] = work;
items[size++] = item;
}
public synchronized Work pop() {
while (index == 0) {
public synchronized E pop() {
while (size == 0) {
try {
this.wait();
} catch (InterruptedException e) {}
}
this.notifyAll();
return works[--index];
return (E) items[--size];
}
}
package org.hongxi.java.util.concurrent;
/**
* @author shenhongxi 2019/8/13
*/
public class BlockingStackTest {
public static void main(String[] args) {
BlockingStack<String> stack = new BlockingStack<>(10);
new Thread(new Producer(stack, "p1")).start();
new Thread(new Consumer(stack, "c1")).start();
new Thread(new Producer(stack, "p2")).start();
new Thread(new Consumer(stack, "c2")).start();
}
static class Consumer implements Runnable {
private BlockingStack stack;
private String name;
public Consumer(BlockingStack stack, String name) {
this.stack = stack;
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 60; i++) {
Object o = stack.pop();
System.err.println(name + " consume: " + o);
try {
Thread.sleep((long) (Math.random() * 400));
} catch (InterruptedException e) {}
}
}
}
static class Producer implements Runnable {
private BlockingStack stack;
private String name;
public Producer(BlockingStack stack, String name) {
this.stack = stack;
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 60; i++) {
String data = name + "-" + i;
stack.push(data);
System.out.println(name + " produce: " + data);
try {
Thread.sleep((long) (Math.random() * 100));
} catch (InterruptedException e) {}
}
}
}
}
package org.hongxi.java.util.concurrent.sync;
/**
* @author shenhongxi 2019/8/11
*/
public class Consumer implements Runnable {
private WorkStack stack;
private String name;
public Consumer(WorkStack stack, String name) {
this.stack = stack;
this.name = name;
}
public String getName() {
return name;
}
@Override
public void run() {
for (int i = 0; i < 60; i++) {
Work work = stack.pop();
System.out.println(getName() + "消费" + work);
try {
Thread.sleep((long) (Math.random() * 400));
} catch (InterruptedException e) {}
}
}
}
package org.hongxi.java.util.concurrent.sync;
/**
* @author shenhongxi 2019/8/11
*/
public class Main {
public static void main(String[] args) {
WorkStack stack = new WorkStack();
new Thread(new Producer(stack, "p1")).start();
new Thread(new Consumer(stack, "c1")).start();
new Thread(new Producer(stack, "p2")).start();
new Thread(new Consumer(stack, "c2")).start();
}
}
package org.hongxi.java.util.concurrent.sync;
/**
* @author shenhongxi 2019/8/11
*/
public class Producer implements Runnable {
private WorkStack stack;
private String name;
public Producer(WorkStack stack, String name) {
this.stack = stack;
this.name = name;
}
public String getName() {
return name;
}
@Override
public void run() {
for (int i = 0; i < 60; i++) {
Work work = new Work(i);
stack.push(work);
System.out.println(getName() + "生产" + work);
try {
Thread.sleep((long) (Math.random() * 100));
} catch (InterruptedException e) {}
}
}
}
package org.hongxi.java.util.concurrent.sync;
/**
* @author shenhongxi 2019/8/11
*/
public class Work {
private int id;
public Work(int id) {
this.id = id;
}
public int getId() {
return id;
}
@Override
public String toString() {
return "Work" + getId();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册