写了一个,根据你的要求,只是我让他满的时候就停了,可以根据自己的需求修改
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;public class Test {
public static void main(String[] args) { int length = 5; // 假设5个就满
List<Long> list = new ArrayList<Long>(length);
thread1(list,length);
thread2(list,length);
} public static void thread1(final List<Long> list, final int length) {
new Thread() {
public void run() {
while (list.size() < length) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.add(System.currentTimeMillis()); // 每隔一秒存一个时间,直到存满为止
System.out.println("放入一个元素");
}
}
}.start();
} public static void thread2(final List<Long> list, final int length) {
new Thread() {
public void run() {
boolean over = true;
while (over) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (list.size() >= length) {
System.out.println("list满,清空开始");
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
list.removeAll(list);
System.out.println("清空list,list大小为:"+list.size());
over = false;
}
else{
System.out.println("没有满");
}
}
}
}.start();
}
}为了体现异步性,我每隔1秒存一个时间,每隔0.7秒检查有没有满,运行结果如下:
没有满
放入一个元素
没有满
放入一个元素
没有满
没有满
放入一个元素
没有满
放入一个元素
没有满
没有满
放入一个元素
list满,清空开始
1208099198575
1208099199575
1208099200575
1208099201575
1208099202575
清空list,list大小为:0
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;public class Test {
public static void main(String[] args) { int length = 5; // 假设5个就满
List<Long> list = new ArrayList<Long>(length);
thread1(list,length);
thread2(list,length);
} public static void thread1(final List<Long> list, final int length) {
new Thread() {
public void run() {
while (list.size() < length) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.add(System.currentTimeMillis()); // 每隔一秒存一个时间,直到存满为止
System.out.println("放入一个元素");
}
}
}.start();
} public static void thread2(final List<Long> list, final int length) {
new Thread() {
public void run() {
boolean over = true;
while (over) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (list.size() >= length) {
System.out.println("list满,清空开始");
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
list.removeAll(list);
System.out.println("清空list,list大小为:"+list.size());
over = false;
}
else{
System.out.println("没有满");
}
}
}
}.start();
}
}为了体现异步性,我每隔1秒存一个时间,每隔0.7秒检查有没有满,运行结果如下:
没有满
放入一个元素
没有满
放入一个元素
没有满
没有满
放入一个元素
没有满
放入一个元素
没有满
没有满
放入一个元素
list满,清空开始
1208099198575
1208099199575
1208099200575
1208099201575
1208099202575
清空list,list大小为:0
public static void main(String[]args){
Add add = new Add();
Del del = new Del();
Thread t = new Thread(add);
t.start();
t = new Thread(del);
t.start();
}
}
class Add implements Runnable{ public synchronized void run() {
while(true){
if(ThreadTest.linkedList.size() < 10){
ThreadTest.linkedList.addLast("1");
System.out.println(Thread.currentThread().getName() + ":当前队列大小" + ThreadTest.linkedList.size());
}
}
}
}
class Del implements Runnable{ public synchronized void run() {
while(true){
if(ThreadTest.linkedList.size() == 10){ //假设队列大小为10
ThreadTest.linkedList.clear();
System.out.println(Thread.currentThread().getName() + ":队列已满,清空队列");
}
}
}
}
void run()
{
while(true)
{
if(满)
{
thread2.notify();
wait();
}
else
{ 添加}
}
}synchronized void clear()
{
this.notify();
}
另外一个线程同理有wait() 和notify() 在另外一个线程中执行了清空后直接调用thead1.clear()
public class Test {
public static void main(String[] args) {
Queue q=new Queue();
Producer p=new Producer(q);
Customer c=new Customer(q);
new Thread(p).start();
new Thread(c).start();
}
}
class Producer implements Runnable{
Queue q;
public Producer(Queue q){
this.q=q;
}
public void run() {
for(int i=0;i<10;i++){ //这里按需要自己改
q.put(i);
System.out.println("Producer puts:"+i);
}
}
}
class Customer implements Runnable{
Queue q;
public Customer(Queue q){
this.q=q;
}
public void run() {
while(true){
System.out.println("Customer gets:"+q.get());
}
}
}
class Queue{ private int value;
private boolean full=false;
public synchronized void put(int i){
if(!full){
value=i;
full=true;
notify();
}
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized int get(){
if(!full){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
full=false;
notify();
return value;
}
}