<pre name="code" class="html">package com.citibank.aimy.Examples.serialize; import java.util.ArrayList; public class ProductBox { private ArrayList ls = new ArrayList(); private int index = 0; //create 4 products one time. public synchronized void push(){ if(index>=8) try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("produce a new product:::"+index); // Product p = new Product(index); // ls.add(p); // System.out.println("add a new product:::"+((Product)ls.get(index)).getId()); index++; this.notify(); } public synchronized void push(Product p){ if(index==8)// try { this.wait(); System.out.println("Full"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } ls.add(p); p.setPlace(String.valueOf(index)); System.out.println("Produce::::::"+p); index++; this.notify(); } public synchronized void pop(){ if(index==0) try { this.wait(); System.out.println("Empty"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Product r = (Product) ls.remove(index-1); System.out.println("Consume:::::::"+r); index--; this.notify(); } } </pre><br/><span id="_xhe_temp" width="0" height="0" /><pre name="code" class="html">package com.citibank.aimy.Examples.serialize; import java.util.ArrayList; public class ProductBox { private ArrayList ls = new ArrayList(); private int index = 0; //create 4 products one time. public synchronized void push(){ if(index>=8) try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("produce a new product:::"+index); // Product p = new Product(index); // ls.add(p); // System.out.println("add a new product:::"+((Product)ls.get(index)).getId()); index++; this.notify(); } public synchronized void push(Product p){ if(index==8)// try { this.wait(); System.out.println("Full"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } ls.add(p); p.setPlace(String.valueOf(index)); System.out.println("Produce::::::"+p); index++; this.notify(); } public synchronized void pop(){ if(index==0) try { this.wait(); System.out.println("Empty"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Product r = (Product) ls.remove(index-1); System.out.println("Consume:::::::"+r); index--; this.notify(); } } </pre><br/><span id="_xhe_temp" width="0" height="0" /> package com.citibank.aimy.Examples.serialize; public class Consumer extends Thread { private ProductBox b; public Consumer(ProductBox box){ this.b = box; } public void run(){ for(int i=0;i<10;i++){ b.pop(); } try { Thread.sleep((long) (Math.random()*2000)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package com.citibank.aimy.Examples.serialize; public class Product { public Product(String id){ this.id = id; } private String place; private String name; private String color; private String scale; private String id; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getScale() { return scale; } public void setScale(String scale) { this.scale = scale; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } public String toString(){ String ret = "this product id:::: "+this.id +" ,is placed in "+this.place; return ret; } } package com.citibank.aimy.Examples.serialize; public class Producer extends Thread { private ProductBox b; public Producer(ProductBox box){ this.b = box; } public void run(){ for(int i=0;i<10;i++){ Product p = new Product(String.valueOf(i)); b.push(p); } try { Thread.sleep((long) (Math.random()*200)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package com.citibank.aimy.Examples.serialize; public class MainThread { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ProductBox b = new ProductBox(); Producer p = new Producer(b); Consumer c = new Consumer(b); p.setPriority(Thread.MAX_PRIORITY); p.start(); c.start(); // Product[] ls = new Product[6]; // System.out.print(ls.length); } }