面试时要求写一个生产者消费者模型,特在此记录一下
给对象加锁:
package newthread;
import java.util.*;
class Counter{
List<Integer> lists = new ArrayList<>();
int size = 10;
Object object = new Object();
public void add() throws InterruptedException {
synchronized (object){
while (lists.size() >= size){
System.out.println("队列满了");
object.wait();
}
lists.add(1);
System.out.println(Thread.currentThread().getName() + " 加入了数据, 当前列表大小为 " + lists.size());
object.notifyAll();
}
}
public void del() throws InterruptedException {
synchronized (object){
while (lists.size() == 0){
System.out.println("队列空了");
object.wait();
}
lists.remove(0);
System.out.println(Thread.currentThread().getName() + " 获取了数据, 当前列表大小为 " + lists.size());
object.notifyAll();
}
}
}
class Provider implements Runnable {
Counter counter;
public Provider(Counter counter){
this.counter = counter;
}
@Override
public void run() {
while (true) {
try {
counter.add();
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable{
Counter counter;
public Consumer(Counter counter){
this.counter = counter;
}
@Override
public void run() {
while (true){
try {
counter.del();
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ProviderConsumer{
public static void main(String[] args) {
Counter counter = new Counter();
Provider provider = new Provider(counter);
Consumer consumer = new Consumer(counter);
Thread provider1 = new Thread(provider, "provider1");
Thread provider2 = new Thread(provider, "provider2");
Thread consumer1 = new Thread(consumer, "consumer1");
Thread consumer2 = new Thread(consumer, "consumer2");
provider1.start();
provider2.start();
consumer1.start();
consumer2.start();
}
}
给方法加锁
import java.util.ArrayList;
import java.util.List;
class Counter {
List<Integer> lists = new ArrayList<>();
int size = 10;
public synchronized void add() throws InterruptedException {
while (lists.size() >= size) {
System.out.println("队列满了");
wait();
}
lists.add(1);
System.out.println(Thread.currentThread().getName() + " 加入了数据, 当前列表大小为 " + lists.size());
notifyAll();
}
public synchronized void del() throws InterruptedException {
while (lists.size() == 0) {
System.out.println("队列空了");
wait();
}
lists.remove(0);
System.out.println(Thread.currentThread().getName() + " 获取了数据, 当前列表大小为 " + lists.size());
notifyAll();
}
}
class Provider implements Runnable {
Counter counter;
public Provider(Counter counter) {
this.counter = counter;
}
@Override
public void run() {
while (true) {
try {
counter.add();
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
Counter counter;
public Consumer(Counter counter) {
this.counter = counter;
}
@Override
public void run() {
while (true) {
try {
counter.del();
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ProviderConsumer {
public static void main(String[] args) {
Counter counter = new Counter();
Provider provider = new Provider(counter);
Consumer consumer = new Consumer(counter);
Thread provider1 = new Thread(provider, "provider1");
Thread provider2 = new Thread(provider, "provider2");
Thread consumer1 = new Thread(consumer, "consumer1");
Thread consumer2 = new Thread(consumer, "consumer2");
provider1.start();
provider2.start();
consumer1.start();
consumer2.start();
}
}