Online appointment book
Download
1 / 36

Online Appointment Book - PowerPoint PPT Presentation


  • 138 Views
  • Updated On :

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Online Appointment Book' - nedaa


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Online appointment book l.jpg
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.


Slide2 l.jpg

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.


Use case l.jpg
Use Case 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

  • 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


Threads l.jpg
Threads 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

  • 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


Run method l.jpg
Run 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 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


Two ways l.jpg
Two Ways 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

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


Slide11 l.jpg

public class SimpleThread extends Thread { 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

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

}

}


Slide12 l.jpg

public class TwoThreadsTest { 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

public static void main (String[] args) {

new SimpleThread("Jamaica").start();

new SimpleThread("Fiji").start();

}

}


Slide13 l.jpg

import java.awt.Graphics; 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

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

}

}


Slide14 l.jpg

public void run() { 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

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

}

}

}


Slide15 l.jpg

public void paint(Graphics g) { 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

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

}

}


Life cylce of a thread l.jpg
Life Cylce of a Thread 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


Life cycle l.jpg
Life Cycle 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

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


Stopping l.jpg
Stopping 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

  • run method that terminates gracefully.

  • No stop method (as in Applet)

  • Condition for termination.

  • Dead condition


Priority l.jpg
Priority 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

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


Slide22 l.jpg

public class RaceTest { 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

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

}

}


Synchronizing threads l.jpg
Synchronizing Threads 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

  • Shared Data

  • Critical Sections: same object accessed by separate, concurrent threads.

  • synchronized keyword

  • Java associates a lock with every object within synchronized code


Slide24 l.jpg

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

* @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.


Slide25 l.jpg

class Bank 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

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

}


Slide26 l.jpg

public void test() /*how much in each account?*/ 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

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

}


Slide27 l.jpg

class TransactionSource extends Thread 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

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

}


Cubbyhole example l.jpg
Cubbyhole Example 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


Slide29 l.jpg

public class Producer extends Thread { 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

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

}

}

}


Slide30 l.jpg

public class Consumer extends Thread { 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

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

}

}

}


Locking an object l.jpg
Locking an Object 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

  • 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


Slide34 l.jpg

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.


Thread communication l.jpg
Thread Communication contain the

  • 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


Slide36 l.jpg

  • wait() contain the : 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.