CBT UnitII Mod

Embed Size (px)

Citation preview

  • 8/12/2019 CBT UnitII Mod

    1/18

    Prepared by M.S.Abirami and M.Uma

    CS0356 Component Based Technologies

    UNIT II Java Based Component TechnologiesThreads

    Threads are a way for a program to fork or split itselfinto two or more simultaneously running tasks. On a single

    processor, Multithreading occurs by time-division multiplexing(time slicing); the processor switches between differentthreads. On a multiprocessor or multi-core system, multiplethreads can run simultaneously on different processors orcores.

    State Transition Diagram Refer the book Pg.No. 280

    Java defines a model of concurrency andsynchronization. The unit of concurrency is a thread. Threadsare passive. Threads can only communicate through

    synchronization. A thread executes statements and moves fromobject to object as it executes method invocations. Threads can

    be dynamically created, pre-empted, suspended, resumed, andterminated. Each thread belongs to a thread group specified atcreation time. Threads belonging to separate thread groups can

    be mutually protected against thread state modifications.Threads can be assigned to one of ten priority levels and can bein either user or demon mode.

    Javas threads are lightweight as, on any given virtual machine,

    they all execute in the same address space. A JVM is free toschedule processing resources by pre-empting executingthreads at any time. For consistent access to larger units, suchas multiple objects, explicit synchronization is required.

    Java supports thread synchronization either by asynchronized method or by synchronized statement.Synchronization forces a thread to acquire a lock on an object

    before proceeding. If a thread already holds a lock on thatobject can continue. This avoids deadlocks- a thread re-entering an object.

    A thread waiting for an event would have to pollvariables. To avoid inefficiencies, Java allows threads to waiton any object, either indefinitely or up to some specifiedtimeout. So class Object defines methods wait, notify, and

    notifyAll. These methods can only be called by a thread thatholds a lock on that object. While waiting, the lock is released.On notification, a thread reacquires the lock before proceeding.If multiple threads have been notified then they will competefor the lock and acquire it.

    Objects that own a thread are instances of a subclass ofclass java.lang.Thread.

  • 8/12/2019 CBT UnitII Mod

    2/18

    Prepared by M.S.Abirami and M.Uma

    public class Thread implements Runnable {

    public static Thread currentThread(); public static void yield();

    public static void sleep(long ms) throws Interrupted Exception;

    public Thread(); //default constructor; use method run

    public Thread (Runnable runObj); // use method run of runObj

    public void run();

    public void start();

    public void stop(); //deprecated

    public void suspend();//deprecated

    public void resume();//deprecated public void interrupt();

    public static boolean interrupted();

    public boolean isInterrupted();

    public void setPriority(int newPriority);

    public void serDaemon(Boolean on);

    public final Boolean isDaemon();

    public void destroy(); //unsafe, not implemented

    }

    A thread object can itself define the outermost method to beexecuted by the new thread method run in class Thread.

    Alternatively, the thread object is constructed with a referenceto an object that implements interface Runnable. In this, threadexecution starts by calling the run method of that other object.The thread terminates either if the outermost method returns orwhen its outermost method throws an exception.

    Thread that are waiting for a lock or sleeping can beinterrupted by another thread, which causes

    InterrupedException to be thrown. A thread can be terminated by calling its owing objects stop or destroy methods. Methodstop sends a ThreadDeath exception, whereas destroyterminates the thread with no clean-up. Stopping a threadcauses that the thread to release all its locks (locked objects ininconsistent state), whereas objects lockded by a destroyedthread remain locked for good.

    JavaBeansIn Java, visual programming is supported via the "Javabean"API. The application builder tool loads the beans into a"toolbox" or "palette". You can select a bean from the toolbox,drop it into a "form", modify its appearance or properties, anddefine its interaction with other beans. Using the JavaBeanscomponent technology, you can compose (or assemble) an

    application with just a few lines of codes."A Javabean is a reusable software component that can bemanipulated visually in an application builder tool."

    "A Javabean is an independent, reusable software component.Beans may be visual object, like Swing components (e.g.JButton, JTextField) that you can drag and drop using a GUI

    builder tool to assemble your GUI application. Beans may also

  • 8/12/2019 CBT UnitII Mod

    3/18

  • 8/12/2019 CBT UnitII Mod

    4/18

  • 8/12/2019 CBT UnitII Mod

    5/18

  • 8/12/2019 CBT UnitII Mod

    6/18

  • 8/12/2019 CBT UnitII Mod

    7/18

  • 8/12/2019 CBT UnitII Mod

    8/18

  • 8/12/2019 CBT UnitII Mod

    9/18

  • 8/12/2019 CBT UnitII Mod

    10/18

  • 8/12/2019 CBT UnitII Mod

    11/18

  • 8/12/2019 CBT UnitII Mod

    12/18

  • 8/12/2019 CBT UnitII Mod

    13/18

  • 8/12/2019 CBT UnitII Mod

    14/18

  • 8/12/2019 CBT UnitII Mod

    15/18

  • 8/12/2019 CBT UnitII Mod

    16/18

    Prepared by M.S.Abirami and M.Uma

    Lab Exercise

    Problem Statement: Demonstrate implementation ofRMI to add 2 numbers.

    Program 1:

    Step1 : Create the remote interface (server side interface)

    Note: All remote interfaces must extend the Remote interface,which is the part of java.rmi. This Remote interface defines

    no members. Its purpose is simply to indicate that an interfaceuses remote methods. All remote methods can throw aRemoteException .

    // remote interface

    import java.rmi.*;

    public interface addinter extends Remote {double add(double d1, double d2) throws RemoteException; }

    Program 2:

    Step 2: Create the implementation class (server side class) andupdate in RMI registry

    Note: The second step can be written either in 2 programs eachfor implementation class and RMI registry or can

    be done in single program. All remote objects mustextend UniCastRemoteObject , which providesfunctionality that is needed to make objects availablefrom remote machines. Updation in RMI registry on that

    remote machine is done by using the rebind() method of

    the Naming class ( found in java.rmi). Thisrebind() method has two argument one is server file

    name, second is reference to an instance of server object.

    // remote implementation and updation in registry

    import java.rmi.*;

    import java.rmi.server.*;

    public class Addserver extends UnicastRemoteObjectimplements add

    {

    public Addserver() throws RemoteException{}

    public static void main(String args[]) throws Exception

    {

    System.out.println(Server starts);

    Addserver s = new Addserver();

    Naming.rebind(Addserver,s);

    }

    public double add(double x, double y)

    { return(x+y);} }

  • 8/12/2019 CBT UnitII Mod

    17/18

    Prepared by M.S.Abirami and M.Uma

    Program 3:

    Step 3: Create the client application

    Note: This program implements the client side of thisdistributed application. The URL consist of RMI prtocol and IPaddress of the server machine and server name. The lookup() method of the Naming class is used by the URL and, returns areference to an object of type addtwo. So, all the rmi can bedirected to this object.

    // client.java

    import java..io.*;

    import java.rmi.*;

    import java.net.*;

    public class Addclient

    {

    public static void main(String args[]) throws Exception

    { InputStreamReader isr = new

    InputStreamReader(System.in);BufferedReader b1 = new BufferedReader(isr);

    InetAddress ia = InetAddress.getLocalHost();

    String ip = ia.toString.substring(ia.toString().indexOf(/)+1);

    String url = rmi://+ip+server;

    System.out.println(Remote method invocation );

    Addinter a = (Addinter)Naming.lookup(url);System.out.println(enter two numbers to add);

    String temp = b1.readline();

    int a = Integer.parseInt(temp);

    String temp1 = b1.readline();

    int b = Integer.parseInt(temp1);

    System.out.println(addition of two numbers = + a.add(a,b));

    } }

    (Compile all the programs)

    Step 4: Now use rmic to generate the stub and skeletonC :> rmic verbose Addserver

    This command generate Addserver_Skel.class andAddserver_stub.class. The java 2 does not require skeletonclass. To create stub alone use the following command:

    C:> rmic v1.2 AddserverStep 5: Start the rmiregistry (run on default port 1099)

    C :> start rm rmiregistry

    Step 6: Run the server program followed by client program

  • 8/12/2019 CBT UnitII Mod

    18/18

    Prepared by M.S.Abirami and M.Uma

    Note:1. Content within () is for your reference. No need to

    prepare.

    2. RMI-IIOP: Refer class notes

    3. Refer your text book for Figures.