代码如下:public class ThreadTest4 {
public static void main(String[] args) {
Queue q = new Queue();
Producer p = new Producer(q);
Consumer c = new Consumer(q);
new Thread(p).start();
new Thread(c).start();
}
}
class Producer implements Runnable{
Queue q;
public Producer(Queue q){
this.q = q;
}
@Override
public void run() {
for(int i = 1; i< 10; i++){
q.put(i);
System.out.println("put : " + i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable{
Queue q;
public Consumer(Queue q){
this.q = q;
}
@Override
public void run() {
for(int i = 1; i < 10; i++){
System.out.println("get : " + q.get());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Queue{
int value;
boolean isFull = false;
public synchronized void put(int i){
if(isFull){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
value = i;
isFull = true;
notify();
}
public synchronized int get(){
if(!isFull){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
isFull = false;
notify();
return value;
}
}运行结果会出现先get,后put的情况,请大神指出错误,不胜感激!
public static void main(String[] args) {
Queue q = new Queue();
Producer p = new Producer(q);
Consumer c = new Consumer(q);
new Thread(p).start();
new Thread(c).start();
}
}
class Producer implements Runnable{
Queue q;
public Producer(Queue q){
this.q = q;
}
@Override
public void run() {
for(int i = 1; i< 10; i++){
q.put(i);
System.out.println("put : " + i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable{
Queue q;
public Consumer(Queue q){
this.q = q;
}
@Override
public void run() {
for(int i = 1; i < 10; i++){
System.out.println("get : " + q.get());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Queue{
int value;
boolean isFull = false;
public synchronized void put(int i){
if(isFull){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
value = i;
isFull = true;
notify();
}
public synchronized int get(){
if(!isFull){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
isFull = false;
notify();
return value;
}
}运行结果会出现先get,后put的情况,请大神指出错误,不胜感激!
public class ThreadDemo03 { public static void main(String[] args) {
Stack s = new Stack();
Producer p1 = new Producer(s);
Producer p2 = new Producer(s);
Producer p3 = new Producer(s);
Consumer c1 = new Consumer(s);
p1.start();
p2.start();
p3.start();
c1.start();
}}class Producer extends Thread {
private Stack s; public Producer(Stack s) {
this.s = s;
} public void run() {
for (int i = 0; i < 1000; i++) {
synchronized (s) {
if (s.getPointer() < (s.getCapacity() - 1)) {
String goods = "goods" + (s.getPointer() + 1);
s.push(goods);
System.out.println(goods + " push into stack");
}
} try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
}class Consumer extends Thread {
private Stack s; public Consumer(Stack s) {
this.s = s;
} public void run() {
for (int i = 0; i < 1000; i++) {
synchronized (s) {
if (s.getPointer() > 0) {
String goods = s.pop();
System.out.println(goods + " pop from stack");
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
}class Stack {
private int point = -1;
private String[] buf = new String[100]; public Stack() { } public int getCapacity() {
return 100;
} public void push(String goods) {
point++;
buf[point] = goods;
} public String pop() {
String tmp = buf[point];
buf[point] = null;
point--;
return tmp;
} public int getPointer() {
return point;
}
}