
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















Overview
Implement online voting for a Terp Idol contest
Networking Streams Multithreading
Terp Idol Online Voting
Must use a client-server model Use threads to handle multiple clients Use synchronization to avoid data races
Contest, CommandProcessor Server, Client, Judge, Voter
Set up server
Set up clients
Handle client-server requests
A client can send requests (in the form of strings) tothe Server through its Socket connection
Server examines request from client. Two requests(PING, DISCONNECT) are handled directly
Other requests are passed to the CommandProcessor
The response for each request (if any) is passed backto the client as a string
Server continues to serve Client until DISCONNECT isreceived. Server then goes back to waiting for clients.
Requests & Responses Sent by Client Sends Server Returns PING SERVER RUNNING CONTESTANTS CONTESTANTS
Client Programming
Determine server location – IP address & port
Open network connection to server
Write data to server (request)
Read data from server (response)
Close network connection
Stop client
Simple Server Programming
Determine server location - port (& IP address)
Create ServerSocket to listen for connections
Loop while (true) { Accept network connection from client Read data from client (request) Write data to client (response) Close network connection to client }
ServerSocket Class
Server listens to port
Begin waiting after invoking accept() Listen for connection (from client socket) Returns Socket for connection
Server Example public class Server { public static void main(String args[ ]) throws Exception { ServerSocket ss = new ServerSocket(4242); while (true) { Socket s = ss.accept(); BufferedReader r = new BufferedReader( new InputStreamReader(s.getInputStream())); PrintWriter out = new PrintWriter( new OutputStreamWriter(s.getOutputStream())); String name = r.readLine(); out.println("Hello " + name); out.flush(); s.close(); } } }
URL Class
To resource named by URL
Creating Threads in Java
Define class implementing Runnable interface public interface Runnable { public void run( ); }
Put work to be performed in run( ) method
Create instance of the “worker” class
Create thread to run it Create Thread object Pass worker object to Thread constructor Or hand the worker instance to an executor Alternative methods for running threads
Lock
Entity can be held by only one thread at a time
A type of synchronization Used to enforce mutual exclusion Thread can acquire / release locks Thread will wait to acquire lock (stop execution) If lock held by another thread
Lock Example public class DataRace extends Thread { static int common = 0; static Object o; // all threads use o’s lock public void run() { synchronized(o) { // single thread at once int local = common; // data race eliminated local = local + 1; common = local; } } public static void main(String[] args) { o = new Object(); … } }