-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc4da67
commit fe9af85
Showing
7 changed files
with
99 additions
and
129 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
whatsmars-common/src/test/java/org/hongxi/java/util/concurrent/BlockingStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.hongxi.java.util.concurrent; | ||
|
||
/** | ||
* @author shenhongxi 2019/8/13 | ||
*/ | ||
public class BlockingStack<E> { | ||
private static final int DEFAULT_CAPACITY = 10; | ||
|
||
Object[] items; | ||
int size; | ||
|
||
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(); | ||
items[size++] = item; | ||
} | ||
|
||
public synchronized E pop() { | ||
while (size == 0) { | ||
try { | ||
this.wait(); | ||
} catch (InterruptedException e) {} | ||
} | ||
this.notifyAll(); | ||
return (E) items[--size]; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
whatsmars-common/src/test/java/org/hongxi/java/util/concurrent/BlockingStackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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) {} | ||
} | ||
} | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
whatsmars-common/src/test/java/org/hongxi/java/util/concurrent/sync/Consumer.java
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
whatsmars-common/src/test/java/org/hongxi/java/util/concurrent/sync/Main.java
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
whatsmars-common/src/test/java/org/hongxi/java/util/concurrent/sync/Producer.java
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
whatsmars-common/src/test/java/org/hongxi/java/util/concurrent/sync/Work.java
This file was deleted.
Oops, something went wrong.
30 changes: 0 additions & 30 deletions
30
whatsmars-common/src/test/java/org/hongxi/java/util/concurrent/sync/WorkStack.java
This file was deleted.
Oops, something went wrong.