Online Appointment Book Implement a Client/Server application for an online appointment book. Client...

36
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.

Transcript of Online Appointment Book Implement a Client/Server application for an online appointment book. Client...

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

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.

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

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.

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

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

Page 4: Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
Page 5: Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
Page 6: Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
Page 7: Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.

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

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

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

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

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.

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

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()); } }

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

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

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

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(); } }

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

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 } } }

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

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; } }

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

Life Cylce of a Thread

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

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).

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

Stopping

• run method that terminates gracefully.

• No stop method (as in Applet)

• Condition for termination.

• Dead condition

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

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.

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

public class SelfishRunner extends Thread {

private int tick = 1; private int num;

public SelfishRunner(int num) { this.num = num; }

public void run() { while (tick < 400000) { tick++; if ((tick % 50000) == 0) System.out.println("Thread #" + num + ", tick = " + tick); } }}

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

public class PoliteRunner extends Thread {

private int tick = 1; private int num;

public PoliteRunner(int num) { this.num = num; }

public void run() { while (tick < 400000) { tick++; if ((tick % 50000) == 0) { System.out.println("Thread #" + num + ", tick = " + tick); yield(); } } }}

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

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(); }}

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

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

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

/** * @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 Bankand 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 amountof money.

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

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*/ }

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

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;}

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

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;}

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

Cubbyhole Example

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

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) { } } } }

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

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); } } }

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

public class ProducerConsumerTest { public static void main(String[] args) { CubbyHole c = new CubbyHole(); Producer p1 = new Producer(c, 1); Consumer c1 = new Consumer(c, 1);

p1.start(); c1.start(); }}

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

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

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

public class CubbyHole { private int contents; private boolean available = false;

public synchronized int get() { while (available == false) { try { wait(); } catch (InterruptedException e) { } } available = false; notifyAll(); return contents; }

public synchronized void put(int value) { while (available == true) { try { wait(); } catch (InterruptedException e) { } } contents = value; available = true; notifyAll(); }}

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

Note that the method declarations for both put and get contain the synchronized keyword. Hence, the system associates a uniquelock 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.

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

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

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

• 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.