他们共同操作的是size++这个操作,把size++用sychronized锁起来保证他的原子性
size用volatile修饰,保证他的可见性两者一起操作
class MyQueue2 {
private int[] date = new int[100];
private int head = 0;//队首元素的下标
private int tail = 0;//队尾元素的下标
//如果head == tail 则队列满了 因为这是个前闭后开区间
private volatile int size = 0;
//入队列
public void offer(int x) throws InterruptedException {
if(size == date.length){
//满了
synchronized (this){
wait();
}
}
//把新元素放到tail的位置上
date[tail] = x;
tail++;
if(tail == date.length){
tail = 0;
}
synchronized (this) {
size++;
notify();
}
}
//出队列
public synchronized Integer poll() throws InterruptedException {
if(size == 0){
synchronized (this){
wait();
}
}
Integer ret = date[head];
head++;
if(head == date.length){
head = 0;
}
synchronized (this) {
size--;
notify();
}
return ret;
}
public Integer peek(){
if(size == 0){
return null;
}
return date[head];
}
public boolean isEmpty(){
return size == 0;
}
public int size(){
return size;
}
public static void main(String[] args) {
Producer producer = new Producer();
producer.start();
Customer customer = new Customer();
customer.start();
// customer.sleep(100);
// while (true){
// System.out.println(queue2.size());
// }
}
private static MyQueue2 queue2 = new MyQueue2();
private static class Producer extends Thread{
Producer(){
super("生产者");
}
PrintWriter printWriter;
{
try {
printWriter = new PrintWriter("生产者2.txt","UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
Random random = new Random(101);
for (int i = 0; i < 1000; i++){
try {
int val = random.nextInt(20);
queue2.offer(val);
printWriter.println(val);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
printWriter.close();
}
}
private static class Customer extends Thread{
Customer(){
super("消费者");
}
PrintWriter printWriter;
{
try {
printWriter = new PrintWriter("消费者.txt","UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
for (int i = 0; i < 1000; i++){
try {
int val = queue2.poll();
printWriter.println(val);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
printWriter.close();
}
}
}
多消费者情况
class MyQueue2 {
private int[] date = new int[100];
private int head = 0;//队首元素的下标
private int tail = 0;//队尾元素的下标
//如果head == tail 则队列满了 因为这是个前闭后开区间
private volatile int size = 0;
//入队列
public synchronized void offer(int x) throws InterruptedException {
while (size == date.length){
//满了
wait();
}
//把新元素放到tail的位置上
date[tail] = x;
tail++;
if(tail == date.length){
tail = 0;
}
size++;
notify();
}
//出队列
public synchronized Integer poll() throws InterruptedException {
while (size == 0){
wait();
}
Integer ret = date[head];
head++;
if(head == date.length){
head = 0;
}
size--;
notify();
return ret;
}
public Integer peek(){
if(size == 0){
return null;
}
return date[head];
}
public boolean isEmpty(){
return size == 0;
}
public int size(){
return size;
}
public static void main(String[] args) {
Producer producer = new Producer();
producer.start();
Customer customer = new Customer();
customer.start();
// customer.sleep(100);
// while (true){
// System.out.println(queue2.size());
// }
}
private static MyQueue2 queue2 = new MyQueue2();
private static class Producer extends Thread{
Producer(){
super("生产者");
}
PrintWriter printWriter;
{
try {
printWriter = new PrintWriter("生产者2.txt","UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
Random random = new Random(101);
for (int i = 0; i < 1000; i++){
try {
int val = random.nextInt(20);
queue2.offer(val);
printWriter.println(val);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
printWriter.close();
}
}
private static class Customer extends Thread{
Customer(){
super("消费者");
}
PrintWriter printWriter;
{
try {
printWriter = new PrintWriter("消费者.txt","UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
for (int i = 0; i < 1000; i++){
try {
int val = queue2.poll();
printWriter.println(val);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
printWriter.close();
}
}
}