Full HTML for

Basic foilset Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future

Given by Nancy J. McCracken,Geoffrey C. Fox at Peking Tutorial, Web Certificate on Spring-Summer 1997. Foils prepared 18 May 97
Outside Index Summary of Material


In Part 1 2 and 3 of the Tutorial We Covered:
  • Overview including History and alpha versus production issues
  • Comparison of Java and Javascript
  • Overall Java Philosophy and Features including security etc.
  • Java Programming Language
  • Introduction to Applications,Applets and their Invocation from HTML
  • "Hello World" and Basic Graphics Applets
  • Object Oriented and Class Structure
  • Methods, Constructors etc.
  • Interfaces
  • Exceptions
  • Introduction to Threads
  • Graphics in more detail
  • Downloading and Drawing Images
  • Abstract Windowing Toolkit
  • Keyboard and Mouse Events
  • Components, Actions, Layouts
In This Part of the Java Tutorial We Cover:
Threads in Detail
Useful Java Classes
  • Object Math Date String Vector Hashtable
Networking and I/O
Futures and HPCC Implications

Table of Contents for full HTML of Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future

Denote Foils where Image Critical
Denote Foils where HTML is sufficient
denotes presence of Additional linked information which is greyed out if missing

1 Java Tutorial - Spring 1997
Part 4: Multithreading, useful Java classes, I/O, Networking, and the future
http://www.npac.syr.edu/projects/tutorials/Java/

2 Threads are part of the Java Language!
(a more serious discussion than in part III of tutorial)

3 Initial Remarks on Threads
4 How to Use Threads from a Class
5 Thread Execution and Concurrency
6 The Life of a Thread
7 Synchronized Method and Blocks
8 Threads and Synchronization - I
9 Threads and Synchronization - wait()
10 Threads and Synchronization - notify()
11 Threads and Synchronization - Example
12 Useful Basic Java Classes
13 The Overarching Object Class
14 Determining and Testing Class of Object
15 java.lang.Object Wrappers
16 The java.lang.Math class
17 The Date class
18 The String class
19 More on Strings, and the StringBuffer class
20 ReverseString Class
21 The Vector class
22 Methods for Vectors
23 The Hashtable class
24 I/O and the powerful Stream Zoo
25 I/O Streams
26 The Input Stream Zoo
27 FilterInputStreams
28 Networking and Web Access
29 Networking and Web Access in Java
30 Security Concerns for Applets
31 Table for Java file and network access
32 Accessing URL's in Java -- URL, showDocument
33 Accessing URL's in Java -- URLConnection
34 I/O: Reading a File known by URL
35 I/O: Setting up the URL, Layout and Thread
36 I/O: Reading the Text File
37 Socket from the Client Side
38 Sockets from the Server Side
39 Performance
and dreaming about the Future

40 Use of Native Classes to Speed Up Execution
41 Comments on Native C Methods
42 HPCC and Java -- High Performance HPjava -- I
43 HPCC and Java -- HPjava -- II

Outside Index Summary of Material



HTML version of Basic Foils prepared 18 May 97

Foil 1 Java Tutorial - Spring 1997
Part 4: Multithreading, useful Java classes, I/O, Networking, and the future
http://www.npac.syr.edu/projects/tutorials/Java/

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Instructors: Geoffrey Fox ,
Nancy McCracken
Syracuse University
111 College Place
Syracuse
New York 13244-4100

HTML version of Basic Foils prepared 18 May 97

Foil 2 Threads are part of the Java Language!
(a more serious discussion than in part III of tutorial)

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 3 Initial Remarks on Threads

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Java is remarkable for threads being built into the language
Threads are processes which are typically "light-weight" (unlike UNIX processes) which communicate by a combination of shared memory and messaging
  • This communication mechanism is what you get from natural access to methods and variables in Java classes
The Java Threads do not give all you want and for those coming from an HPCC background, we note Java threads have no immediate support for some key parallel computing concepts (see work of Chandy at Caltech) and distributed memory (threads running in separate operating system instances)
Java threads are based on basic monitors ("lock mechanism") for synchronization which was introduced by Hoare

HTML version of Basic Foils prepared 18 May 97

Foil 4 How to Use Threads from a Class

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
One can implement threads in two ways
  • Firstly subclassing the Thread class and overriding its run() method
  • Secondly by creating a Thread with a Runnable object (i. e. that implements Runnable interface)
Only the second way is possible for applets as these already extend Applet class and so cannot multiply inherit from the Thread class
Threads must have a run method which is code Thread executes.
If you are using first way, this is written for this particular thread and overrides run() in Thread class
In second way, the created Thread automatically takes its run() method to be the run method of the applet

HTML version of Basic Foils prepared 18 May 97

Foil 5 Thread Execution and Concurrency

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Threads enable you to have multiple flows of control, running concurrently.
Concurrency does not necessarily mean that threads actually run at the same time, but that the Java interpreter switches between threads.
Threads may also be assigned priority, in which case the higher priority thread may pre-empt the lower one.

HTML version of Basic Foils prepared 18 May 97

Foil 6 The Life of a Thread

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Each thread is always in one of five states, shown in this diagram with the most common methods for changing state:

HTML version of Basic Foils prepared 18 May 97

Foil 7 Synchronized Method and Blocks

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Synchronized methods and blocks are implemented in the applet class by acquiring a lock before running
Code will therefore not run at the same time as other code that needs access to the same resource.
Each object has one lock associated with it and each class has exactly one lock
When invoked, a synchronized method waits until it can acquire the lock for the current instance (i.e., this); a static synchronized method waits to acquire the class lock
Lock is released when code is done executing
Locks are advisory
synchronized blocks specify the object on which to synchronize

HTML version of Basic Foils prepared 18 May 97

Foil 8 Threads and Synchronization - I

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
A Synchronized method provides a guarantee that the method is the only synchronized method in the object running at the time.
This is useful if the resource is contained completely in the object, and in general, no waiting for a resource is necessary. (The operation is contained completely in the method)
  • public synchronized void do_it() {
    • Do_my_stuff(); }
More fine-grained synchronization can be obtained by synchronizing on the particular object:
  • Object obj;
  • public void do_it() {
    • synchronized(obj) {
    • ... change the state of obj ..}
  • ... do other time-consuming stuff ..}

HTML version of Basic Foils prepared 18 May 97

Foil 9 Threads and Synchronization - wait()

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
void wait()
void wait(int timeout)
  • The wait() method is part of the Object class, and hence available for every object.
  • This method simply causes the thread to wait until notified or until the timeout occurs.
  • timeout is an optional argument. If missing or zero, thread waits until notify/notifyall called
  • This method must be called from a synchronized method.
  • wait() will be called while thread owns lock associated with a particular object. wait() releases this lock (atomically i.e. safely)

HTML version of Basic Foils prepared 18 May 97

Foil 10 Threads and Synchronization - notify()

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Threadsafe Variables
void notify()
void notifyAll()
  • The notify() and notifyall() methods are part of the Object class, and hence available for every object.
  • These methods must be called from a synchronized method.
  • These methods simply notifies a thread that is waiting.
notify() notifies the thread associated with given synchronization object that has been waiting the longest time
notifyall() notifies all threads associated with given object
One can mark a variable as "threadsafe" to notify the compiler that only one thread will be modifying this variable.

HTML version of Basic Foils prepared 18 May 97

Foil 11 Threads and Synchronization - Example

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
public class AddApplet extends Applet implements Runnable {
  • Thread t;
  • boolean go_ahead = false;
  • int a, b, totle;
  • public void start() {
    • t = new Thread(this);
    • t.start();
  • }
  • public synchronized void run() {
    • while ( true ) {
    • while ( !go_ahead ) wait(); // wait for notify signal
    • totle = a + b;
    • go_ahead = false;
    • System.out.println("totle = "+totle);
    • }
  • }
  • public synchronized void adder(int i, int j) {
    • a = i; b = j;
    • notify(); // Notify the thread that a,b are ready } }

HTML version of Basic Foils prepared 18 May 97

Foil 12 Useful Basic Java Classes

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 13 The Overarching Object Class

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
public class Object is the root of the Class hierarchy. Every java class has Object as its ultimate parent and so any object (object with a small o is any instance of any class) can use methods of Object
Methods of Object (used as object.Method()) include:
clone() Creates a clone of the object
equals(another object) compares two objects returning boolean answer
getClass() returns a descriptor of type Class (a child of Object) defining class of object
toString() returns a String which represents "value" of object. It is expected that each subclass will override this method
wait() in various forms is used to cause threads to wait
finalize() contains code to perform when object is deleted by system (I.e. garbage collected)

HTML version of Basic Foils prepared 18 May 97

Foil 14 Determining and Testing Class of Object

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Suppose an object is called obj , then we get Class of obj by
Class itsclass = obj.getClass(); // and the name of this class by:
String name = itsclass.getName();
One can also use instanceof in following fashion
"foo" instanceof String // is evaluated to true but
mPoint pt = new mPoint(x,y);
pt instanceof String // is evaluated to false

HTML version of Basic Foils prepared 18 May 97

Foil 15 java.lang.Object Wrappers

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
"types" such as int char float etc. are NOT classes
Thus one cannot use methods such as
int var;
var.toString
Thus ALL types have associated wrappers used like
Character wrappedchar = new Character(char);
now wrappedchar has lots of good methods you can use such as:
wrappedchar.equals(anotherobject);
wrappedchar.toString();
There are also static (class) functions such as:
toUppercase(char ch);
isUpperCase(char ch);

HTML version of Basic Foils prepared 18 May 97

Foil 16 The java.lang.Math class

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
This class provides the standard mathematical functions, using types int, long, float and double as appropriate.
It is a static class, meaning that you only use the methods, never creating objects from this class.
The methods include
  • IEEEremainder, abs, ceil, cos, exp, floor, log, max, min, pow, random, sin, sqrt, and other trig functions.
  • The random number generator is a linear congruential generator, which is fast but not random enough for many scientific applications.

HTML version of Basic Foils prepared 18 May 97

Foil 17 The Date class

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
This class exists to provide an implementation of "date" structures. As is typical of data encapsulation classes, it has methods to create dates, obtain and set the parts of dates, and convert dates to other data types.
The constructor either creates today's date or any other day (and time) that you provide:
  • Date today = new Date();
  • Date ancient = new Date(999,12,31); /* Dec. 31, 999 */
  • Date deadline = new Date(1996,12,31,23,59,59)
    • /* Dec.31,1996 at 23:59:59 */

HTML version of Basic Foils prepared 18 May 97

Foil 18 The String class

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Strings are fixed length collections of Unicode characters stored as an instance of the class.
Most commonly, a string is created from a string literal or by using the constructor on an array of characters:
  • String greeting = "Hello";
or
  • char[] bunch = {'H', 'e', 'l', 'l', 'o'};
  • String greeting = new String(bunch);
Once created, individual characters of a string cannot be changed in place. This example shows using the methods of indexing, catenation (+), and substring to create a new string with one character changed:
  • String test = "Chicken soup with rice";
  • int a = test.indexOf('w');
  • String newtest = substring(1,a-1)+"is n"+substring(a+5);
  • /* getting "Chicken soup is nice" */

HTML version of Basic Foils prepared 18 May 97

Foil 19 More on Strings, and the StringBuffer class

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
String comparison can be done with the methods equals and equalsIgnoreCase. Note that == tests if two strings are the same string instance, while equals tests if two different strings have the same characters.
Other methods include length, CharAt and toString.
The StringBuffer class has mutable strings, but is created with a fixed maximum size. It has methods such as append to extend the length of the string.

HTML version of Basic Foils prepared 18 May 97

Foil 20 ReverseString Class

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
This class returns object of class String which reverses order of characters in input object source which is also an instance of class String
class ReverseString {
  • public static String reverseIt(String source) {
    • int i, len= source.length();
    • StringBuffer dest = new StringBuffer(len);
    • for( i= (len-1); i >=0 ; i--) {
    • dest.append(source.charAt(i));
    • }
    • return dest.toString();
    • }
  • }

HTML version of Basic Foils prepared 18 May 97

Foil 21 The Vector class

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
In Java, while you can give the size of arrays at run time, you cannot dynamically change the size of an array during the computation. The vector class provides a data structure with this property - the restriction is that all of the elements must be of type Object.
  • It is usually simple to insert an element of any type and Java will convert it to an Object. But when you extract an element, you must explicitly cast it to convert it back to the type you want.
A vector is created with an "initial capacity" and a "capacity increment". It always starts with 0 elements. As you add elements, if the initial capacity is exceeded, then more memory is automatically allocated in the size of the capacity increment. The default is an initial capacity of 10 and an increment which doubles each time.
  • Vector shoes = new Vector();
  • Vector orders = new Vector(100, 10);

HTML version of Basic Foils prepared 18 May 97

Foil 22 Methods for Vectors

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Elements are created with the addElement method:
  • Order missouri = new Order();
  • orders.addElement(missouri);
The object missouri of type Order is automatically converted to an Object in vector instance orders defined on previous foil.
There are methods for indexing vectors. Like arrays, the indexing is zero-based.
  • x = (Typeofx) v.elementAt(i);
  • v.setElementAt(x,i);
The length of the Vector can be obtained:
  • int size = v.size;

HTML version of Basic Foils prepared 18 May 97

Foil 23 The Hashtable class

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
This class is similar to the Perl associative array (or hash array with {} brackets). It can store a set of key and value pairs, neither of which can be null.
  • Hashtable staff = new Hashtable();
  • Employee harry = new Employee("Harry Hacker");
  • staff.put("987-98-9996", harry);
Values are retrieved by indexing with a key. Like Vectors, Hashtables only store things of type Object and you must cast the result.
  • steve = (Employee) staff.get("149-26-7355");
If there was no entry, a null is returned.
Performance of the Hashtable can also be affected by giving an initialCapacity and a loadFactor for reallocation.

HTML version of Basic Foils prepared 18 May 97

Foil 24 I/O and the powerful Stream Zoo

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 25 I/O Streams

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Streams are an abstraction of a sequence of bytes.
The sources and destinations of these sequences can be
  • files
  • network connections
  • blocks of memory
That is, all of these types of I/O are treated in the same way.
The most basic I/O streams are InputStream and OutputStream. These classes have methods that can read or write a byte from the stream:
  • int read ();
  • void write (byte);
  • also skip(), available(), close()
There are also methods for reading and writing arrays of bytes.
These methods all "block" on the read or write during the data transfer.

HTML version of Basic Foils prepared 18 May 97

Foil 26 The Input Stream Zoo

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
The subclasses of InputStream offer more methods that can read the stream in a more structured way or provide other functionality.
Standard UNIX file I/O methods are available
FileInputStream s = new FileInputStream("/usr/gcf/greatidea");

HTML version of Basic Foils prepared 18 May 97

Foil 27 FilterInputStreams

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
There is a clever class called FilterInputStream which can be used to add value to a raw InputStream. You can define your own filters but important ones provided are:
  • BufferedInputStream -- establishs an intermediate buffer to service stream
  • DataInputStream -- allows one to address stream in terms of higher level constructs -- namely read a line, read a long integer etc.
  • LineNumberInputStream -- adds line numbers to a stream
  • PushbackInputStream -- allows one to "unread" a character and put it back on the input stream
These streams can be constructed from each other or InputStream to give added functionality. If "is" is an InputStream, then it can be converted to a buffered input stream with methods from datainputstream:
DataInputStream data = new DataInputStream(new BufferedInputSream(is));

HTML version of Basic Foils prepared 18 May 97

Foil 28 Networking and Web Access

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 29 Networking and Web Access in Java

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
This area will evolve rapidly as existing I/O systems get linked to Java with special classes such as those needed to link MPI (HPCC Message Passing) or Nexus (Well known distributed memory thread package)
One can establish Web Connection with URL class and access documents there
One can set up a more general URLConnection for more general input/output operations through Web(HTTP) protocol
One can set up a Socket link which is permanent rather than on again off again Web client-server interaction
One can send messages and so transfer information between different Applets

HTML version of Basic Foils prepared 18 May 97

Foil 30 Security Concerns for Applets

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
One aspect of Java security is language restrictions designed not to let a Java applet or application access memory on the machine outside of its own space.
Applets have additional restrictions:
  • they can never run a local executable program;
  • they cannot communicate with any host other than the server from which they were downloaded (the originating host);
  • they cannot read or write to the local computer's file system, except through the browser mechanism;
  • they cannot find out information about the local computer (see table on next slide for details).
As of summer 1996, no known applets have seriously broken security to steal client information or trash the local disk. Exceptions:
  • applets have been written to use up arbitrary amounts of client cpu.
  • applets with native code can trash the local disk. So far, native code is disallowed on publicly released browsers.

HTML version of Basic Foils prepared 18 May 97

Foil 31 Table for Java file and network access

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
This table shows what Java programs can do in the following four cases:
  • NL - Netscape loading a URL for applet
  • NF - Netscape loading a local file for applet
  • AV - Applet viewer
JA - Java application (not an applet)
    • NL NF AV JA
read local file no no yes yes
write local file no no yes yes
get file information no no yes yes
delete file no no no yes
run another program no no yes yes
read the user.name proper no yes yes yes
connect to network port on server yes yes yes yes
connect to network port on other host no yes yes yes
load Java library no yes yes yes
call exit no no yes yes
create a pop-up window with warning yes yes yes

HTML version of Basic Foils prepared 18 May 97

Foil 32 Accessing URL's in Java -- URL, showDocument

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
First you set the URL in various ways using something like
String npacurl = "http://www.npac.syr.edu/index.html";
try { theinputurl = new URL(npacurl); } // URL class is in java.net
catch ( MalformedURLException e) {
  • System.out.println("Bad URL: " + npacurl); }
where you are meant to test to see if URL is legal!
The simplest thing to do now is to see this page with
getAppletContext().showDocument(theinputurl, Frame targetdisplayframe);

HTML version of Basic Foils prepared 18 May 97

Foil 33 Accessing URL's in Java -- URLConnection

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
More interesting is to access a URL and process the information there!
This is done using streams:
There are methods in class URL --
InputStream in = instanceofURL.openStream(); // opens an InputStream associated with given url
More general is instanceofURL.openConnection() establishes a general connection and returns an instance of class URLConnection
  • This provides most general HTTP connectivity and indeed has some advantages over sockets as these are subject to special security restrictions in Netscape's current implementation
Note that one can connect not just to HTML files but also to CGI scripts i.e. programs at server and so obtain flexible connectivity

HTML version of Basic Foils prepared 18 May 97

Foil 34 I/O: Reading a File known by URL

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
This applet creates a text area in the window, reads text from a file known by its URL, and displays the text in the text area. It uses the class URL from java.net, to set up a socket to the server, and the class InputStream from java.io.

HTML version of Basic Foils prepared 18 May 97

Foil 35 I/O: Setting up the URL, Layout and Thread

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 36 I/O: Reading the Text File

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
The openStream method opens the socket connection and returns an object of type InputStream. We convert this to a BufferedInputStream for performance and then to DataInputStream, where we have methods to read lines or characters of data.

HTML version of Basic Foils prepared 18 May 97

Foil 37 Socket from the Client Side

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
A Java applet or application can open a socket to a Java server application through the Socket class.
  • Socket t = new Socket("internet host name", port)
or Socket t = new Socket(this.getCodeBase().getHost(), port)
The latter form is useful for applets as the applet can only use a socket to a server application running on the web host machine that the applet code was downloaded from.
Sockets have methods getInputStream and getOutputStream which can be used for communication. Note that both communication directions are possible at the same time on a socket.

HTML version of Basic Foils prepared 18 May 97

Foil 38 Sockets from the Server Side

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
A Java application can open a single Socket on a port and open an InputStream and OutputStream just like the client, but in general, a server will want to be able to open multiple sockets to establish communication with a number of clients at the same time.
The ServerSocket class allows multiple sockets:
  • ServerSocket s = new ServerSocket(port);
Then the Java server application can use the accept method to open a socket for each client which tries to open a socket on the port:
  • Socket client = s.accept();
Typically, a server main program would be waiting to accept a client. When a client tries to establish a socket, the accept proceeds. Then the server would spawn a thread to handle that one socket, opening input and output streams as necessary and communicating with the client.

HTML version of Basic Foils prepared 18 May 97

Foil 39 Performance
and dreaming about the Future

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 40 Use of Native Classes to Speed Up Execution

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
One can essentially augment supplied Java Runtime by supplying your own C or other code implementing a particular functionality in a higher performance mode
This of course generates machine dependence and should only be used if really needed
First for classes you wish to implement in native fashion, put in your java code lines like:
public native mustgofast(arguments); // default functions
static { System.loadLibrary("faststuffhere"); } // static and in class and so runs only ONCE! and loads the native code

HTML version of Basic Foils prepared 18 May 97

Foil 41 Comments on Native C Methods

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Advantages
  • May call resources available from C
    • Window/Graphic Systems
    • Database
    • Networking and Other Third Party Libraries
    • Higher performance as compiled (always) and as C (advantage with todays compilers)
Disadvantages
  • Classes with native methods, or library linking calls, may not be downloaded across the network.
  • Libraries may not be downloaded across the network.
  • Programmer must write a native library for every platform used.
So usually user defined native method can not be used remotely in Applet.
  • Can pre-download as a plugin

HTML version of Basic Foils prepared 18 May 97

Foil 42 HPCC and Java -- High Performance HPjava -- I

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
The activities that has gone into defining High Performance Fortran (HPF) and HPC++ can be reexamined for Java which is quite well suited for parallelism as
  • It essentially has task parallelism built in with Applet mechanism
  • As there are no pointers, it is easier to define data and other implicit parallelism so it can be efficiently implemented
Interesting Compiler challenge independent of parallelism, is to produce efficient code from bytecodes. Here a technique called "just in time" compilation does compilation at runtime and can increase performance of Java code to within a factor of 4 (today) to 2 (tomorrow) performance of C
Surely many will produce Fortran C PASCAL to Java translators so you can webify your existing applications
  • This is an alternative to wrapper technology as in native classes

HTML version of Basic Foils prepared 18 May 97

Foil 43 HPCC and Java -- HPjava -- II

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Current runtime (java Threads) assume shared memory but there are interesting possible distributed memory and distributed shared memory implementations
One can imagine a data-parallel interface where methods would implement array and other data parallel operations with distributions specified by annotations as in HPF
  • Here one has a HPJava translator that produces java "plus message passing" code
Important implication for parallel CORBA using Java classes to interface with CORBA
Java based servers will allow data and task parallel Java to implement World Wide compute webs
see http://www.npac.syr.edu/projects/javaforcse

© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Thu Jan 8 1998