WILEY END USER LICENSE AGREEMENT
Go to www.wiley.com/go/eula to access Wileys ebook EULA.
List of Acronyms
APIApplication Programming InterfaceCNICygnus Native InterfaceCORBACommon Object Request Broker ArchitectureDCOMDistributed Component Object ModelDGCDistributed Garbage CollectionFIFOFirst In, First OutGCGarbage CollectorGCCGNU Compiler CollectionGCJGNU Compiler for JavaGNUGNUs Not UnixIIOPInternet Inter-ORB ProtocolIPInternet ProtocolJ2SEJava 2 Standard EditionJ2EEJava 2 Enterprise EditionJ2MEJava 2 Micro EditionJDKJava SE Development KitJNIJava Native InterfaceJRMPJava Remote Method ProtocolJSSEJava Secure Socket ExtensionJVMJava Virtual MachineKVMKilo VMOSIOpen Systems InterconnectionPCPPriority Ceiling ProtocolPIPPriority Inheritance ProtocolRMIRemote Method InvocationRMICRMI CompilerRPCRemote Procedure CallRRLRemote Reference LayerRTSJReal-Time Specification for JavaSSLSecure Sockets LayerTCPTransmission Control ProtocolTLSTransport Layer SecurityUDPUser Datagram ProtocolURLUniform Resource LocatorWinCEWindows Embedded Compact
Appendix
Exercise 1: lifecycle of a thread
Consider the following class:
class Parrot4 extends Thread{ private String cri = null; private int fois = 0; public Parrot4 (String s, int i) { cri = s; fois = i;} public void run() { System.out.println("Thread parrot:" + Thread.currentThread().getName()); for (int n=0; n
class ChatAndLaunchParrot4{ public static void main(String args[]) { Parrot4 parrot = new Parrot4("coco",5); parrot.start(); System.out.println("Thread chatty : " + Thread.currentThread().getName()); for (int n=0; n<15; n++) { try { Thread.sleep(1000);} catch(InterruptedException e) { } System.out.print("Thread parrot isAlive: "+ parrot.isAlive()+"\t"); blabla(); } } private static void blabla() { System.out.println("blabla"); }}
Question: Launch the execution of the above program.
What is the name of the primary thread, what is the name given by default to the other thread? What is the point of the isAlive() method? Hint: the class method currentThread() returns a pointer on the object Thread, which calls this method.
Exercise 2: properties of different threads
Consider the following two classes:
class Parrot5 extends Thread { private String cri = null; private int fois = 0; public Parrot5(String s, int i) { super("parrot"); cri = s; fois = i;}public void run(){ afficheThreads(); for (int n=0; n< nbrThread ; i++) System.out.println(i + "-ieme Thread: " + tabThread[i].getName()); }}
class ChatAndLaunchParrot5 { public static void main(String args[]) { Thread.currentThread().setName("chatty"); Parrot5 parrot = new Parrot5("coco",15); parrot.start(); for (int n=0; n<5; n++) { try { Thread.sleep(1000); } catch(InterruptedException e) { } blabla(); } } private static void blabla() { System.out.println("blabla"); } }
Hint: The method setName (String name) makes it possible to name a thread. The Thread builder offers the possibility to name it as follows: Thread (String name).
The class method activeCount () gives the number of active threads in the calling threads group.
The class method enumerates (Thread[] table) stores in the given table the references of the active threads in the calling threads group and sub-groups. It returns the number of active threads obtained.
Question: launch the execution of the previous program.
The procedure displayThreads() is called before and after the loop for the method run().
The active threads before the loop were:
Number 0 Thread: chatty
1st Thread: parrot
After the loop for, they became:
Number 0 Thread: parrot
1st Thread: DestroyJavaVM
Explain this result, what is DestroyJavaVM?
Exercise 3: four counters in parallel
The objective for this exercise is to create four counters executing in parallel.
Each "counter" has a name (Toto for example) and it counts from 1 to 10. It pauses randomly between each number (0 to 5000 milliseconds for example). Each counter displays a number (for example, Toto will display, "Toto: 3"); n displays a message such as "Toto has finished counting to 10" when it has finished.
The counter class possesses one single attribute, which is the name of the String-type counter.
Write the counter class and test it by launching 4 that count up to 10. See which one finishes first (the four counters are named Tata, Titi, Toto, Tutu).
Make 2 versions: one where the threads are created with a Thread daughter-class, and one where they are created with an instance of a separate class that implements Runnable.
Exercise 4: resource in mutual exclusion
Consider the following three java classes:
public class Printer1 { private String text; public Printer1() { text="";} public void print(String t) { text=t; for (int j=0;j
public class Prog56 { public static void main (String argv[]) { Writer2 writerA, writerB; Printer1 print= new Printer1(); writerA = new Writer2("ABC", print); writerB = new Writer2("XYZ", print); writerA.start(); writerB.start(); }}
public class Writer2 extends Thread { private String text; private Printer1 print; public Writer2(String t, Printer1 i) { print=i; text=t; } public void run() { for (int i=0; i<10; i++) { print.print(text); try { sleep((long)(Math.random() * 100));} catch (InterruptedException e) {} } // end for System.out.println("writer of " +text+" has finished"); }}
- In the class Printer1, describe the behavior of the print method.
- Describe the behavior of a thread of type Writer2 (explain the run() method).
- Launch the execution of Prog56. Why is the result of the prints illegible?
- Change in the class Printer1 the signature of the print method to public synchronized void print (String t) and restart the execution of Prog56.
- What is the point of the synchronized method?
Exercise 5: program a task with an initial delay and a periodicity
We wish to teach a student the multiplication table for 7, and this is why we want to program a task that displays the multiplication table of 7 every 4 seconds and with an initial delay of 2 seconds. The result is obtained in the following form:
Do you still want to learn the multiplication table? (y/n) The multiplication table for 7 is:
1 * 7 = 7
2 * 7 = 14
3 * 7 = 21
4 * 7 = 28
5 * 7 = 35
6 * 7 = 42
7 * 7 = 49
8 * 7 = 56
9 * 7 = 63
10 * 7 = 70
PS: Any answer other than y cancels the programming.
Exercise 6: shared variables: internal class
Consider the two following classes (the class Parrot20 is called internal class because it is located inside the class MathsParrots20), and there is a single java file called MathsParrots20.java.
public class MathsParrots20{ private int counter; public static void main(String args[]) { new MathsParrots20(); } public MathsParrots20() { counter = 1; Parrot20 parrotA = new Parrot20("coco", 10); Parrot20 parrotB = new Parrot20("hello", 10); parrotA.start(); parrotB.start(); try { parrotA.join(); parrotB.join(); } catch(InterruptedException e) { } System.out.println("counter = "+counter); } class Parrot20 extends Thread { private String cri = null; private int fois = 0; public Parrot20(String s, int i) { cry = s; times = i; } public void repeat() { String repeat = cry + " " + counter; System.out.println(repeat); counter++; try { Thread.sleep((int)(Math.random()*1000)); } catch(InterruptedException e) { } } public void run(){ for (int n=0; n
- Launch and examine the execution.
- Hint: join() is used in order to display the value of the counter before the end of the two threads. Due to the visibility rules in Java, the counter variable is visible/accessible from the Parrot20 class and therefore for both parrotA and parrotB threads. The two threads therefore access a shared space common to all variables, unlike the processes that possess their own work space separate from other processes.