ServerSocket
class that behaves like a factory
(i.e., constructs and returns an object).
Question
below uses the two
classes TickReader
and TickWriter
. Trace
the execution of this application by writing numbers/letters next to
each line indicating the thread it is executed in and the order in
which it is executed. Lines in the first thread should be numbered
using 1,2,3... Lines in the second thread (if there is one) should
be numbered using A,B,C... Lines in the third thread (if there is
one) should be numbered using a,b,c...
import java.io.*; /** * The application * */ public class Question { /** * The entry point of the application * * @param args The array of command-line arguments */ public static void main(String[] args) { BufferedReader br; FileReader fr; PrintWriter pw; TickReader in; TickWriter file, screen; try { fr = FileReader("question.tik"); br = new BufferedReader(fr); in = new TickReader(br); pw = new PrintWriter(System.out, true); screen = new TickWriter(pw); in.setTickObserver(screen); in.readTick(); screen.start(); in.readTick(); } catch (Exception exception) { System.err.println(exception); } } }
import java.io.*; import java.util.*; /** * A class for reading Tick information from a BufferedReader * */ public class TickReader { protected BufferedReader reader; protected TickWriter observer; /** * Construct a new TickReader * * @param reader The BufferedReader to read from */ public TickReader(BufferedReader reader) { this.reader = reader; observer = null; } /** * Set the observer * * @param observer The new observer */ public void setTickObserver(TickWriter observer) { this.observer = observer; } /** * Notify the observer of a Tick * * @param tick The tick to notify them of */ public void notifyTickObserver(String tick) { observer.handleTickNotification(tick); } /** * Read one Tick object and notify the observer * */ public void readTick() throws IOException { String tick; tick = reader.readLine(); notifyTickObserver(tick); } }
import java.io.*; import java.util.*; /** * A class for writing Tick information to a PrintWriter */ public class TickWriter implements Runnable { protected LinkedList queue; protected PrintWriter writer; protected Thread ctrl; /** * Construct a new TickWriter * * @param Writer The BufferedWriter to use */ public TickWriter(PrintWriter writer) { this.writer = writer; queue = new LinkedList(); ctrl = new Thread(this) } /** * Handle notification from a TickReader * * @param tick The tick to be handled */ public void handleTickNotification(String tick) { queue.addLast(tick); } /** * Print the tick on the writer * * @param tick The tick to print */ private void printTick(String tick) { writer.println(tick); } /** * Code to run in the control thread */ public void run() { String tick; if (queue.size() > 0) { tick = (String)(queue.removeFirst()); printTick(tick); } } } /** * Starts the control Thread */ public void start() { ctrl.start(); }The file
question.tik
contains
the following information:
MSFT,STOCK,129.175,b C1997Z,FUTURE,253.3,a MSFT,STOCK,130.175,s
Write a class named Searcher
for this purpose. It will
be passed a CopyOnWriteArrayList
of
String
objects, each of refers to JMU. It must
construct a synchronized
List
to hold
the String
objects that refer to Prof. Bernstein.
The Searcher
object must search through the
CopyOnWriteArrayList
for String
objects
that contain "Bernstein"
. It must use 10 different
threads of execution to conduct the search, each of which must
search through a distinct portion of
the CopyOnWriteArrayList
. Each time such
a String
is found it must be added to the
synchronized List
.
The Searcher
object must have a start()
method that starts the process (and returns immediately). It must
also have a getResults()
method that returns the
synchronized List
when all of the threads have
completed their work, blocking until that is the case.
CopyOnWriteArrayList
an appropriate choice for the
List
of String
objects? What
additional thread-safety does it provide over
a String[]
? How necessary is that additional safety
in this case?
List
for the results? How does a synchronized List
provide
that thread-safety? What other kind of thread-safe Collection
might be used instead?
Printer
class:
public class Printer implements Runnable { private String s; public Printer(String s) { this.s = s; } public void run() { System.out.println(s); } }
list all possible outputs for each of the following fragments (assuming that no threads are interrupted).
Thread a, b; a = new Thread(new Printer("A")); try { a.join(); System.out.println("M"); } catch (InterruptedException ie) { // Shouldn't get here }
Thread a, b; a = new Thread(new Printer("A")); a.start(); try { a.join(); System.out.println("M"); } catch (InterruptedException ie) { // Shouldn't get here }
Thread a, b; a = new Thread(new Printer("A")); a.start(); b = new Thread(new Printer("B")); b.start(); try { a.join(); System.out.println("M"); } catch (InterruptedException ie) { // Shouldn't get here }
Thread a, b; a = new Thread(new Printer("A")); a.start(); b = new Thread(new Printer("B")); b.start(); try { a.join(); b.join(); System.out.println("M"); } catch (InterruptedException ie) { // Shouldn't get here }
join()
method).
Callable
interface have over objects implementing the
Runnable
interface.
File
class and the
Files
class.
ClipOrganizer
that is given a file
name, reads all of the "lines" in that file (each of which is no
more than 80 characters long and is terminated by the operating
system's line termination character(s)), and constructs
a List
of String
objects in each of the
following ways.
BufferedReader
.
Files
class.
SeekableByteChannel
.
MovieMarketer
class.
This class has a single method with the following signature:
public static String getMOTD()
that returns the "movie of the day". The server need only handle one connection at a time.
If the client receives a prompt of "Net:" it must respond with a
reply of "Sandra". If the client receive a prompt of "Fish:" it
must respond with a reply of "swordfish". If the client receives
any other prompt it must reply with a string that contains
12 characters, two of which must be the null character (i.e.,
'\0'
) followed by the ampersand character (i.e.,
'&'
). The client will always be prompted exactly
twice, but the prompts can occur in any order.
The server must generate exactly two prompts, one of which must be "Net:" or "Fish:", the other of which must be something else. It must generate two random numbers, one to determine which prompt is first and the other to determine whether "Net:" or "Fish:" is used. The server must be able to handle multiple connections at the same time.
(1) |
_____
|
In Java, the Socket and ServerSocket classes
use a SocketImplFactory to facilitate
customization.
|
(2) |
_____
|
In Java, the DatagramSocket class is
declared final . |
Copyright 2020