Linguaggi Corso M-Z - Laurea in Ingegneria Informatica A.A. 2009-2010 Alessandro Longheu http://www.diit.unict.it/users/alongheu alessandro.longheu@diit.unict.it Esercitazione I/O, thread, socket in Java 1
Esempio 1 File.ZIP Realizzare un programma che, dato in ingresso il nome di una directory, crei un file.zip e lo invii successivamente via rete ad un server che lo salva sul proprio file system Realizzare l applicazione prima con un socket connection-oriented (socket stream), poi con un socket connection-less (datagram) Esempi su creazione file zip nella directory (8), mentre per la creazione di socket datagram directory (3) 2
Esempio 2 - Serializzazione Serializability of a class is enabled by the class implementing the java.io.serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable. During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream. When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object. 3
Esempio 2 - Serializzazione For an object to be serialized, it must be an instance of a class that implements either the Serializable or Externalizable interface. Both interfaces only permit the saving of data associated with an object's variables. They depend on the class definition being available to the Java Virtual Machine at reconstruction time in order to construct the object. The Serializable interface relies on the Java runtime default mechanism to save an object's state. Writing an object is done via the writeobject() method. Reading the serialized object is accomplished using the readobject() method. What about other objects that may be referred to by the object we are serializing? For instance, what if our object is a Frame containing a set of (AWT) Panel and TextArea instance variables? Using the Serializable interface, these references (and their associated data) also are converted and written to the stream. All state information necessary to reconstruct our Frame object and any objects that it references gets stored together. If those other objects or their formats weren't stored, our reconstructed Frame would contain null object references, and the content of those Panels and TextAreas would be gone. Plus, any methods that rely on the existence of the Panels or TextAreas would throw exceptions. 4
Esempio 2 - Serializzazione The Externalizable interface specifies that the implementing class will handle the serialization on its own, instead of relying on the default runtime mechanism. This includes which fields get written (and read), and in what order. The class must define a writeexternal() method to write out the stream, and a corresponding readexternal() method to read the stream. Inside of these methods the class calls ObjectOutputStream writeobject(), ObjectInputStream readobject(), and any necessary write<datatype>() and read<datatype>() methods, for the desired fields. Esempio di serializzazione nella directory (9) 5
Esempio 2 - Serializzazione Creare un programma che permetta la gestione di un log di accessi ad un sistema: Definire la struttura di base del singolo log Scegliere la struttura dati per l insieme dei log Permettere di serializzare la struttura dati cosi definita, potendo scegliere di serializzare la struttura dati sia in formato testo, che binario, che utilizzando la serializzazione Controllare infine e stampare l occupazione di memoria su disco delle tre possibilità del punto precedente 6
Esempio 2 Diversi esempi sulla programmazione con i Thread e con Socket: Esempio generale di impostazione thread (directory (1)) Esempio generale di socket (directory (2)) Esempio di socket stream e datagram (directory (3)) Particolarità sui thread (directory (4)) problema cinque filosofi + due varianti (directory (5)) problema cinque filosofi senza deadlock (directory (6)) daemon con redirection compito 260107 (directory (7)) 7
Esempio 3 Si realizzi un programma che lancia n thread per cercare un elemento in una matrice di n per n elementi. Ognuno dei thread cerca l'elemento in una delle righe della matrice. Non appena un thread ha trovato l'elemento cercato, rende note agli altri thread le coordinate dell'elemento e tutti i thread terminano. Risolvere il problema dei cinque filosofi 8