Presentation is loading. Please wait.

Presentation is loading. Please wait.

Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.

Similar presentations


Presentation on theme: "Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application."— Presentation transcript:

1 Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application. The server should be capable of handling multiple appointment books and clients. Appointment times should be stored in a file.

2 Let's use student advising as an example. Each faculty member has a set of times that he is available for advising. This information is stored in a file that is identified by faculty member. Students start an application that will access the appointment book server. They identify their advisor, and a list of possible appointment times is given. The student enters their name for the given time, and this appointment is stored in a file.

3 Use Case A use case is a theme for interacting with the system Various external entities interact with the system Use system in different ways Use case drives discovery of classes and associations Use cases can translate into methods

4

5

6

7 Threads A thread is a single sequential flow of control within a program A thread is not a program but runs within a program. Lightweight process: Runs in context of full-blown program and uses its resources Execution Context: Carves out its own resources within the program

8

9 Run Method Gives Thread something to do Implements Thread’s running behavior Thread class implements a default thread that does nothing Its run() method is empty Override to implement a runnable object’s behavior

10 Two Ways Thread class in java.lang Subclass Thread and implement run method Implement the Runnable interface Both require that the run method (what the thread does at runtime) be implemented by your class.

11 public class SimpleThread extends Thread { public SimpleThread(String str) { super(str); //sets name of thread } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + " " + getName()); try { sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) {} } System.out.println("DONE! " + getName()); }

12 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji").start(); }

13 import java.awt.Graphics; import java.util.*; import java.text.DateFormat; import java.applet.Applet; public class Clock extends Applet implements Runnable { private Thread clockThread = null; public void start() { if (clockThread == null) { clockThread = new Thread(this, "Clock"); clockThread.start(); }

14 public void run() { Thread myThread = Thread.currentThread() while (clockThread == myThread) { repaint(); try { Thread.sleep(1000); } catch (InterruptedException e){ // the VM doesn't want us to sleep anymore, // so get back to work }

15 public void paint(Graphics g) { // get the time and convert it to a date Calendar cal = Calendar.getInstance(); Date date = cal.getTime(); // format it and display it DateFormat dateFormatter = DateFormat.getTimeInstance(); g.drawString(dateFormatter.format(date), 5, 10); } // overrides Applet's stop method, not Thread's public void stop() { clockThread = null; }

16 Life Cylce of a Thread

17 Life Cycle Create: New Thread state is empty thread object. Gets run method from target “ this ”. start () creates system resources for the thread, schedules thread, and calls run (). Not Runnable if sleep () (time elapses), wait () (notification from another object), or blocked on I/O (I/O completes).

18 Stopping run method that terminates gracefully. No stop method (as in Applet) Condition for termination. Dead condition

19 Priority CPU Scheduling of the thread. Based on fixed priority scheduling setPriority (), inherits priority, priority is an integer from MIN_PRIORITY to MAX_PRIORITY (higher, higher priority) Round-robin for equal priority. yield () method or Time slicing (system). Preemption by higher priority threads.

20

21

22 public class RaceTest { private final static int NUMRUNNERS = 2; public static void main(String[] args) { SelfishRunner[] runners = new SelfishRunner[NUMRUNNERS]; for (int i = 0; i < NUMRUNNERS; i++) { runners[i] = new SelfishRunner(i); runners[i].setPriority(2); } for (int i = 0; i < NUMRUNNERS; i++) runners[i].start(); }

23 Synchronizing Threads Shared Data Critical Sections: same object accessed by separate, concurrent threads. synchronized keyword Java associates a lock with every object within synchronized code

24 /** * @version 1.00 24 Jun 1997 * @author Cay Horstmann */ class SynchBankTest { public static void main(String[] args) { Bank b = new Bank();/*object shared by all transaction sources*/ int i; for (i = 1; i <= Bank.NACCOUNTS; i++) new TransactionSource(b, i).start(); } This is application from command line. Set up a Bank and start transaction threads on each account. Simulate 10 bank accounts. Randomly generate transactions that move money between accounts. There is a thread for each account. Each transaction moves a random amount of money.

25 class Bank { public Bank() { accounts = new long[NACCOUNTS]; int i; for (i = 0; i < NACCOUNTS; i++) accounts[i] = INITIAL_BALANCE; ntransacts = 0; test(); }/*Populate accounts with money*/ /*accounts corrupted if not synchronized*/ public synchronized void transfer(int from, int to, int amount) { while (accounts[from] < amount)/*current thread blocked, gives up lock*/ { try { wait(); } catch(InterruptedException e) {} }/*if not enough money, wait for more*/ accounts[from] -= amount; accounts[to] += amount; ntransacts++; if (ntransacts % 5000 == 0) test(); notifyAll(); /*let everybody check for enough money*/ }

26 public void test() /*how much in each account?*/ { int i; long sum = 0; for (i = 0; i < NACCOUNTS; i++) sum += accounts[i]; System.out.println("Transactions:" + ntransacts + " Sum: " + sum); } public static final int INITIAL_BALANCE = 10000; public static final int NACCOUNTS = 10; private long[] accounts; private int ntransacts; }

27 class TransactionSource extends Thread { public TransactionSource(Bank b, int i) { from = i - 1; bank = b; } public void run() { while (true) { int to = (int)(Bank.NACCOUNTS * Math.random()); if (to == from) to = (to + 1) % Bank.NACCOUNTS; int amount = (int)(Bank.INITIAL_BALANCE * Math.random()); bank.transfer(from, to, amount); try { sleep(1); } catch(InterruptedException e) {} } private Bank bank; private int from; }

28 Cubbyhole Example

29 public class Producer extends Thread { private CubbyHole cubbyhole; private int number; public Producer(CubbyHole c, int number) { cubbyhole = c; this.number = number; } public void run() { for (int i = 0; i < 10; i++) { cubbyhole.put(i); System.out.println("Producer #" + this.number + " put: " + i); try { sleep((int)(Math.random() * 100)); } catch (InterruptedException e) { } }

30 public class Consumer extends Thread { private CubbyHole cubbyhole; private int number; public Consumer(CubbyHole c, int number) { cubbyhole = c; this.number = number; } public void run() { int value = 0; for (int i = 0; i < 10; i++) { value = cubbyhole.get(); System.out.println("Consumer #" + this.number + " got: " + value); }

31

32 Locking an Object Consumer should not access CubbyHole when Producer changing it, and Producer should not access it when Consumer getting value. Object locked until unlocked. Threads must have a way to communicate condition changes to each other

33

34 Note that the method declarations for both put and get contain the synchronized keyword. Hence, the system associates a unique lock with every instance of CubbyHole (including the one shared by the Producer and the Consumer). Whenever control enters a synchronized method, the thread that called the method locks the object whose method has been called. Other threads cannot call a synchronized method on the same object until the object is unlocked. So, when the Producer calls CubbyHole's put method, it locks the CubbyHole, thereby preventing the Consumer from calling the CubbyHole's get method: boolean available is true when the value has just been put but not yet gotten and is false when the value has been gotten but not yet put.

35 Thread Communication notify (), notifyAll (), wait () methods from Object get loops until Producer produces new value. wait relinquishes lock on CubbyHole by Consumer waits on notification from Producer

36 wait() : Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. notify() : Wakes up a single thread that is waiting on this object's monitor. notifyAll() : Wakes up all threads that are waiting on this object's monitor.


Download ppt "Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application."

Similar presentations


Ads by Google