import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedList;import java.util.Queue;import java.util.Set;
public class MemberMain implements Runnable {
// 作为缓冲区存放对象 private Set setMember = new HashSet();
// private Boolean isWriter = true; // private Integer []i = new Integer[3];//{1,2,3,4,5,6,7,8,9}; // i = {1,2,3,4,5,6,7,8,9}; private int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; private static ArrayList list = new ArrayList();
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub
// m.operateXml(); MemberMain m = new MemberMain(); // Product p = new Product(); // Custom c = new Custom(); // // 运行写线程;0-9写到一个容器中,最多放5个; // new Thread(p).start(); // // 运行读线程;a/b依次取出数据; // new Thread(c).start();
// 同一个对象的不同线程; new Thread(m, "writeThread").start(); new Thread(m, "readThread").start();
}
public int[] getArr() { return arr; }
public synchronized void run() { String thName = Thread.currentThread().getName(); System.out.println("thName =" + thName);
if (thName.equals("writeThread")) { for (int i = 0; i < arr.length; i++) { list.add(arr[i]); if (list.size() > 4) { // try { // Thread.sleep(1000); // } catch (InterruptedException e) { // e.printStackTrace(); // }
try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } if (list.size() > 0) { System.out.println("list1 =" + list); notify(); }
} else { if (null != list && list.size() > 0) {
Iterator iter = list.iterator(); while (iter.hasNext()) { int i = (Integer) iter.next(); System.out.println("i =" + i); iter.remove();
} System.out.println("list2 =" + list);
if (list.size() == 0) { notify(); try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
}
