*** BUFFER *** import java.util.*; import java.io.*; class Producer implements Runnable { private Buffer buffer; public Producer(Buffer b) { buffer=b; } public void run() { try { Reader in = new FileReader ("t.txt"); for (int k=0; k<=10; k++) { int ch; ch = in.read(); buffer.put((char)ch); } in.close(); } catch (Exception e){ System.out.println("Error" + e.toString()); } } } class Consumer implements Runnable { private Buffer buffer; public Consumer(Buffer b) { buffer=b; } public void run() { for(int i=0; i<250; i++) { System.out.println(buffer.get()); } } } public class Buffer { private char[] buf; private int last; public Buffer(int sz) { buf = new char[sz]; last=0; } public boolean isFull() { return(last==buf.length); } public boolean isEmpty() { return(last==0); } public synchronized void put(char c) { while(isFull()) { try{ wait(); } catch(InterruptedException e) { } } buf[last++]=c; notify(); } public synchronized char get() { while(isEmpty()) { try{ wait(); } catch(InterruptedException e) { } } char c=buf[0]; System.arraycopy(buf,1,buf,0,--last); notify(); return c; } public static void main(String args[]) { Buffer buf=new Buffer(100); Runnable prod=new Producer(buf); Runnable cons=new Consumer(buf); new Thread(prod).start(); new Thread(cons).start(); try {System.in.read();}catch(Exception e){}; } } *** PING-PONG *** public class pingpong extends Thread { String word; int delay; pingpong(String whatToSay, int delayTime) { word=whatToSay; delay=delayTime; } public void run() { try { for( ; ; ) { System.out.println(word + " "); sleep(delay); } } catch(InterruptedException e) { return; } } public static void main(String[] args) { new pingpong("ping",5000).start(); new pingpong("pong",10000).start(); } }