??????.???????ó???
?????????????Object.wait()??Object.notify()?????????????????????-??????????
public class Test {
private int queueSize = 10;
private PriorityQueue<Integer> queue = new PriorityQueue<Integer>(queueSize);
public static void main(String[] args)  {
Test test = new Test();
Producer producer = test.new Producer();
Consumer consumer = test.new Consumer();
producer.start();
consumer.start();
}
class Consumer extends Thread{
@Override
public void run() {
consume();
}
private void consume() {
while(true){
synchronized (queue) {
while(queue.size() == 0){
try {
System.out.println("???п?????????");
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
queue.notify();
}
}
queue.poll();          //?????????????
queue.notify();
System.out.println("??????????????????????"+queue.size()+"?????");
}
}
}
}
class Producer extends Thread{
@Override
public void run() {
produce();
}
private void produce() {
while(true){
synchronized (queue) {
while(queue.size() == queueSize){
try {
System.out.println("????????????п?????");
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
queue.notify();
}
}
queue.offer(1);        //??β?????????
queue.notify();
System.out.println("???????в???????????????????"+(queueSize-queue.size()));
}
}
}
}
}
???????????????????-????????????????????к?Object.wait()??Object.notify()????wait()??notify()??????????????????
????????????????????wait??notify????????????????л???????
???????????????????????????????-??????????
public class Test {
private int queueSize = 10;
private ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(queueSize);
public static void main(String[] args)  {
Test test = new Test();
Producer producer = test.new Producer();
Consumer consumer = test.new Consumer();
producer.start();
consumer.start();
}
class Consumer extends Thread{
@Override
public void run() {
consume();
}
private void consume() {
while(true){
try {
queue.take();
System.out.println("??????????????????????"+queue.size()+"?????");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer extends Thread{
@Override
public void run() {
produce();
}
private void produce() {
while(true){
try {
queue.put(1);
System.out.println("???????в???????????????????"+(queueSize-queue.size()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
????????з??????????????д????????????????????????????????????????
?????????????У?????????????????У?????????????????????????????????
??????????????t?????????socket???????????????????????????????????????????У?????????????????????????????????????????????????????????-?????????????????????????С?