package TT;
import java.util.LinkedList;
import java.util.Optional;
import org.apache.tomcat.jni.Thread;
public class Mycontainer1<T> {
final private LinkedList<T> lists=new LinkedList<>();
final private int MAX=10;//最多10个元素
private int count=0;
private static Thread thread;
public synchronized void aput(T t){
while(lists.size()==MAX){
try {
this.wait();//effective java
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lists.add(t);
++count;
this.notifyAll();//通知消费者 进行 消费
}
public synchronized T get(){
T t= null;
while (lists.size()==0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
t=lists.removeFirst();
count--;
this.notifyAll();
return t;
}
---------------------------------------------------------------------------------分割线-------------------------
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* @author lcj
* 消费者 生产者
*
* @param <T>
*/
public class MyContainer2<T> {
final private LinkedList<T> lists = new LinkedList<T>();
final private int MAX = 10;// 最多是个10元素
private int count = 0;
private Lock lock = new ReentrantLock();
private Condition producer = lock.newCondition();
private Condition consumer = lock.newCondition();
public void put(T t) {
try {
lock.lock();
while (lists.size() == MAX) {
producer.await();
}
lists.add(t);
++count;
consumer.signalAll();
} catch (Exception e) {
} finally {
lock.unlock();
}
}
public T get() {
T t = null;
try {
lock.lock();
while (lists.size() == 0) {
consumer.await();
}
t = lists.removeFirst();
count--;
producer.signalAll();
} catch (Exception e) {
} finally {
lock.unlock();
}
return t;
}
public static void main(String[] args) {
MyContainer2<String> cont = new MyContainer2<String>();
// 启动消费者线程
for (int i = 0; i < 10; i++) {
new Thread(() -> {
for (int j = 0; j < 5; j++) System.out.println(cont.get());
}, "C" + i).start();
}
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 启动生产者线程
for (int i = 0; i < 2; i++) {
new Thread(() -> {
for (int j = 0; j < 25; j++) {
cont.put(Thread.currentThread().getName() + "" + j);
System.out.println(Thread.currentThread().getName() + "S" + j);
}
}, "S" + i).start();
}
}
}
import java.util.LinkedList;
import java.util.Optional;
import org.apache.tomcat.jni.Thread;
public class Mycontainer1<T> {
final private LinkedList<T> lists=new LinkedList<>();
final private int MAX=10;//最多10个元素
private int count=0;
private static Thread thread;
public synchronized void aput(T t){
while(lists.size()==MAX){
try {
this.wait();//effective java
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lists.add(t);
++count;
this.notifyAll();//通知消费者 进行 消费
}
public synchronized T get(){
T t= null;
while (lists.size()==0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
t=lists.removeFirst();
count--;
this.notifyAll();
return t;
}
---------------------------------------------------------------------------------分割线-------------------------
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* @author lcj
* 消费者 生产者
*
* @param <T>
*/
public class MyContainer2<T> {
final private LinkedList<T> lists = new LinkedList<T>();
final private int MAX = 10;// 最多是个10元素
private int count = 0;
private Lock lock = new ReentrantLock();
private Condition producer = lock.newCondition();
private Condition consumer = lock.newCondition();
public void put(T t) {
try {
lock.lock();
while (lists.size() == MAX) {
producer.await();
}
lists.add(t);
++count;
consumer.signalAll();
} catch (Exception e) {
} finally {
lock.unlock();
}
}
public T get() {
T t = null;
try {
lock.lock();
while (lists.size() == 0) {
consumer.await();
}
t = lists.removeFirst();
count--;
producer.signalAll();
} catch (Exception e) {
} finally {
lock.unlock();
}
return t;
}
public static void main(String[] args) {
MyContainer2<String> cont = new MyContainer2<String>();
// 启动消费者线程
for (int i = 0; i < 10; i++) {
new Thread(() -> {
for (int j = 0; j < 5; j++) System.out.println(cont.get());
}, "C" + i).start();
}
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 启动生产者线程
for (int i = 0; i < 2; i++) {
new Thread(() -> {
for (int j = 0; j < 25; j++) {
cont.put(Thread.currentThread().getName() + "" + j);
System.out.println(Thread.currentThread().getName() + "S" + j);
}
}, "S" + i).start();
}
}
}