online appointment book
Skip this Video
Download Presentation
Online Appointment Book

Loading in 2 Seconds...

play fullscreen
1 / 36

Online Appointment Book - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
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
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.

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

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




public class TwoThreadsTest {

public static void main (String[] args) {

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

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




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





public void run() {

Thread myThread = Thread.currentThread()

while (clockThread == myThread) {


try {


} catch (InterruptedException e){

// the VM doesn\'t want us to sleep anymore,

// so get back to work





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;



life cycle
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).
  • run method that terminates gracefully.
  • No stop method (as in Applet)
  • Condition for termination.
  • Dead condition
  • 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.

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



for (int i = 0; i < NUMRUNNERS; i++)




synchronizing threads
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


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


class Bank

{ public Bank()

{ accounts = new long[NACCOUNTS];

int i;

for (i = 0; i < NACCOUNTS; i++)

accounts[i] = INITIAL_BALANCE;

ntransacts = 0;


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


if (ntransacts % 5000 == 0) test();

notifyAll(); /*let everybody check for enough money*/



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;



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;



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


System.out.println("Producer #" +

this.number + " put: " + i);

try {

sleep((int)(Math.random() * 100));

} catch (InterruptedException e) { }





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




locking an object
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

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