Vous êtes sur la page 1sur 1

A multithreaded program contains two or more parts that can run concurrently, Each part a program is called thread

and each part that defines a separate path of excution. Thus multithreading is a specified from of multitasking . There are two distinct types of multitasking . Process: A Process is , in essence , a program that is executing. Process-based :is heavy weight- allows you run two or more programs concurrently. Eg: you can use JAVA compiler at the same time you are using text editor. Here a program is a small unit of code that can be dispatched by scheduler . Thread-based: is Light weight- A Program can perform two or more tasks simultaneously. Creating a thread: Eg: A text editor can formate at the same time you can print, as long as these two tasks are being perform separate treads. Thread: can be defined as single sequential flow of control with in a program. Single Thread : Application can perform only one task at a time. Multithreaded : A process having more than one thread is said to be multithreaded. The multiple threads in the process run at the same time, perform different task and interact with each other. Daemon Thread : Is a low priority thread which runs immedeatly on the back ground doing the Garbage Collection operation for the Java Run time System. SetDaemon( ) is used to create DaemonThread.

Thread Priorities : are used by the thread scheduler to decide when each thread should ne allowed to run.To set a thread priority, use te setpriority( ), which is a member of a thread. final void setpriority(int level) - here level specifies the new priority seting for the calling thread. The value level must be with in the range :MIN_PRIORITY = 1 NORM_PRIORITY = 5 MAX_PRIORITY = 10 You can obtain the current priority setting by calling getpriority( ) of thread. final int getpriority( ) Synchronization : Two or more threads trying to access the same method at the same point of time leads to synchronization. If that method is declared as Synchronized , only one thread can access it at a time. Another thread can access that method only if the first threads task is completed. Synchronized statement : Synchronized statements are similar to Synchronized method. A Synchronized statements can only be executed after a thread has acquired a lock for the object or Class reffered in the Synchronized statements. The general form is - Synchronized(object) { class SimpleThread extends Thread { public void run() { for ( int count = 0; count < 4; count++) System.out.println( "Message " + count + "From: Mom" ); } } class TestThread { public static void main( String[] args ) { SimpleThread parallel = new SimpleThread(); System.out.println( "Create the thread"); parallel.start(); System.out.println( "Started the thread" ); System.out.println( "End" ); } } class SecondMethod implements Runnable { public void run() { for ( int count = 0; count < 4; count++) System.out.println( "Message " + count + " From: Dad"); class TestThread { public static void main( String[] args ) { SecondMethod notAThread = new SecondMethod(); Thread parallel = new Thread( notAThread ); System.out.println( "Create the thread"); parallel.start(); System.out.println( "Started the thread" ); System.out.println( "End" ); } }

Inter Thread Communication : To Avoid pooling , Java includes an elegant interprocess communication mechanisim. Wait( ) - tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor & call notify( ). notify( ) - wake up the first thread that called wait( ) on the same Object. notifyall( ) wake up all the threads that called wait( ) on the same Object. The highest priority thread aill run fast. Serialization : The process of writing the state of Object to a byte stream to transfer over the network is known as Serialization. Deserialization : and restored these Objects by deserialization. Externalizable : is an interface that extends Serializable interface and sends data into strems in compressed format. It has two methods WriteExternal(Objectoutput out) ReadExternal(objectInput in) Bite code: Is a optimized set of instructions designed to be executed by Java-run time system, which is called the Java Virtual machine (JVM), i.e. in its standard form, the JVM is an Interpreter for byte code. JIT- is a compiler for Byte code, The JIT-Complier is part of the JVM, it complies byte code into executable code in real time, piece-by-piece on demand basis. Final classes : String, Integer , Color, Math Abstract class : Generic servlet, Number class variable:An item of data named by an identifier.Each variable has a type,such as int or Object,andascope class variable :A data item associated with a particular class as a whole--not with particular instances of the class. Class variables are defined in class definitions. Also called a static field. See also instance variable. instance variable :Any item of data that is associated with a particular object. Each instance of a class has its own copy of the instance variables defined in the class. Also called a field. See also class variable. local variable :A data item known within a block, but inaccessible to code outside the block. For example, any variable defined within a method is a local variable and can't be used outside the method. class method :A method that is invoked without reference to a particular object. Class methods affect the class as a whole, not a particular instance of the class. Also called a static method. also instance method. instance method :Any method that is invoked with respect to an instance of a class. Also called simply a method. See also class method. class Loop { public void doLoop( int size, String name ) { for ( int count = 0; count < size; count++) { System.out.println( count + " From: " + name ); double wasteTime = Math.sin( Math.cos ( count ) );

2.

Creating a Thread : 1. By implementing the Runnable Interface. By extending the thread Class. Thread Class : Java.lang.Threadclass is used to construct and access the individual threads in a multithreaded application. Syntax: Public Class <class name> extends Thread { } The Thread class define several methods . Getname() obtain a thread name. Getname() obtain thread priority. Start( ) - start a thread by calling a Run( ). Run( ) - Entry point for the thread. Sleep( ) - suspend a thread for a period of time. IsAlive( ) - Determine if a thread is still running. Join( ) - wait for a thread to terminate. Runable Interface : The Runnable interface consist of a Single method Run( ), which is executed when the thread is activated. When a program need ti inherit from another class besides the thread Class, you need to implement the Runnable interface. Syntax: public void <Class-name> extends <SuperClass-name> implements Runnable Eg: public Class myapplet extends Japplet implements Runnable { // Implement the Class }

o o o o o o o

} }

} }}

class SyncLoop extends Loop { public synchronized void doLoop( int size, String name ) { for ( int count = 0; count < size; count++) { System.out.println( count + " From: " + name ); double wasteTime = Math.sin( Math.cos( count ) ); } } public void unprotected() { System.out.println( "From unprotected" ); } } class Cheat extends Loop { SyncLoop myReference; public Cheat( SyncLoop aReference ) { myReference = aReference; } public void doLoop( int size, String name ) { for ( int count = 0; count < size; count++) myReference.unprotected(); }}

Vous aimerez peut-être aussi