7# שיעור  JAVA
							
1.  public class EchoThread extends Thread {
          private String message;
          private long delay;
          public EchoThread(String message, long delay) {
               this.message=message;
               this.delay=delay;
          }
          public void run() {
               while (true) {
                    System.out.println(message);
                    try {
                         sleep(delay);
                    } catch(InterruptedException ie) {
                    }
               }
          }
          public static void main(String[] args) {
               Thread t1 = new EchoThread("Good morning..",1000);
               Thread t2 = new EchoThread("How are you today?",
                     2000);
               t1.start();
               t2.start();
          }
     }
2. public class Counter implements Runnable {
          private int count;
          private long delay;
          private Thread countingThread;
          public Counter(long delay) {
               this.delay=delay;
          }
          public void run() {
               while(true) {
                    System.out.println(count);
                    try {
                         Thread.sleep(delay);
                    } catch (InterruptedException ie) {
                    }
               }
               count++;
          }
          public void startCounting(int from) {
               count=from;
               countingThread = new Thread(this);
               countingThread.start();
          }
          public void stopCounting() {
               countingThread.stop();
               countingThread = null;
          }
          public static void main(String[] args) {
               Counter counter = new Counter(1000);
               counter.startCounting(1);
          }
     }
3. class Devil extends Thread {
          Devil () {
               setDaemon(true);
               start();
          }
          public void run() {
               // ...
          }
     }
4. import java.applet.*;
    public class UpdateApplet extends Applet 
          implements Runnable {
          private Thread updateThread;
          int updateInterval = 1000;
          public void run() {
               while(true) {
                    try {
                         Thread.sleep(updateInterval);
                    } catch(InterruptedException e) { }
                    repaint();
               }
          }
          public void start() {
               if (updateThread == null) {
                    updateThread = new Thread(this);
                    updateThread.start();
               }
          }
          public void stop() {
               if (updateThread != null) {
                    updateThread.stop();
                    updateThread = null;
               }
          }
     }
          public void paint(java.awt.Graphics g) {
                g.drawString(new java.util.Date().toString(), 10, 25);
     }
5. public void start() {
          if (updateThread == null) {
               updateThread = new Thread(this);
               updateThread.start();
          }
          else updateThread.resume();
    }
     public void stop() {
          updateThread.suspend();
     }
    public void destroy() {
          if (updateThread != null) {
               updateThread.stop();
               updateThread = null;
          }
     }
6. public class BankAccount {
          private double balance;
          public synchronized void deposit(double amount) {
               balance+=amount;
          }
          public synchronized void withdraw(double amount) {
               balance-=amount;
          }
          public synchronized double getBalance() {
               return balance;
          }
          // .....
     }
     public void deposit(double amount) {
          synchronized(this) {
               balance+=amount;
          }
     }
7. public class ProductionLine {
          public static void main(String[] args) {
               Queue buffer = new Queue(10);
               Producer producer = new Producer(buffer);
               Consumer consumer = new Consumer(buffer);
               producer.start();
               consumer.start();
          }
     }
     public class Producer implements Runnable {
          Queue queue;
          public Producer(Queue queue) { 
               this.queue;
          }
          public void run() {
               while(true) {
                    Object item = produceNextItem();
                    // ... wastes a lot of time ...
                    queue.enque(item);
               }
          }
          public void start() {
               engineThread = new Thread(this);
               engineThread.start();
          }
          // .....
     }
     public class Consumer implements Runnable {
          Queue queue;
          public Consumer(Queue queue) { 
               this.queue;
          }
          public void run() {
               while(true) {
                    Object item = queue.deque;
                    // do something with the item
               }
          }
          public void start() {
               engineThread = new Thread(this);
               engineThread.start();
          }
     }
     public class Queue {
          public synchronized void enque(Object item) {
               while (size>=capacity) {
                    try {
                         wait();
                    } catch (InterruptedException ie) { }
               }
               // insert the item
               size++;
               notify();
          }
          public synchronized Object deque() {
               while (size == 0) {
                    try {
                         wait();
                    } catch (InterruptedException ie) { }
               }
               // deque the item at the head
               size-;
               notify();
               return item;
          }
     }
8. public class Thready (
          public static void main(String args[]) {
               new MyThread("Foo").start();
               new MyThread("Bar").start();   }
     }
class MyThread extends Thread {
     String message;
     MyThread (String message) {
          this.message = message;     }
     public void run() {
          while (true) 
               System.out.println(message);   }  
     }
public class Thready {
     public static void main(String args[] ) {
          new MyThread("Foo").start();
          Thread bar = new MyThread("Bar");
          bar.setPriority(Thread.NORM_PRIORITY + 1);
          bar.start();     }
}   
class MyThread extends Thread {   …..
     public void run() {
          while (true) {
               System.out.println(message);
               yield();  }
     }
}