Friday, 24 January 2014

Multi Threaded Programming in java

how to use multithreaded :runnable interface:...

Multithreaded Programming:


Unlike many other computer languages, Java provides built-in support formultithreaded programming. A multithreaded program contains two or more parts that can run concurrently.There are two distinct types of multitasking:process based and thred based.A process.in essence,a program that is executing.
Thus process based multitasking is the features that allow your computer to run two or more program concurrently.Thread based multi tasking environment,tread is the smallest unit of dispatchable code.This means that a single program can perform two or more task simultaneously.

The Java Thread Model


The Java run-time system depends on threads for many things, and all the class libraries are designed with multithreading in mind. In fact, Java uses threads to enable the entire environment to be asynchronous. This helps reduce inefficiency by preventing the waste of CPU cycles.
                             The value of a multithreaded environment is best understood in contrast to its counterpart. Single-threaded systems use an approach called an event loop with polling.In this model, a single thread of control runs in an infinite loop, polling a single event queue to decide what to do next. Once this polling mechanism returns with, say, a signal that a network file is ready to be read, then the event loop dispatches control to the appropriate event handler.
                       Threads exist in several states. A thread can be running.It can be ready to run as soon as it gets CPU time. A running thread can be suspended, which temporarily suspends its activity.A suspended thread can then be resumed, allowing it to pick up where it left off. A thread can be blocked when waiting for a resource.

Thread Priorities


Java assigns to each thread a priority that determines how that thread should be treated with respect to the others. Thread priorities are integers that specify the relative priority of one thread to another .Athread’s priority is used to decide when to switch from one running thread to the next. This is called a context switch. 

 The rules that determine when a context switch takes place are simple:

1) A thread can voluntarily relinquish control. This is done by explicitly yielding, sleeping,or blocking on pending I/O. In this scenario, all other threads are examined, and the highest-priority thread that is ready to run is given the CPU.

2) A thread can be preempted by a higher-priority thread.In this case, a lower-priority thread that does not yield the processor is simply preempted no matter what it is doing by a higher-priority thread. Basically, as soon as a higher-priority thread wants to run, it does. This is called preemptive multitasking.

Synchronization


Because multithreading introduces an asynchronous behavior to your programs, there must be a way for you to enforce synchronicity when you need it. For example, if you want two threads to communicate and share a complicated data structure.
            Java implements an elegant twist on an age-old model of interprocess synchronization: themonitor.Most multithreaded systems expose monitors as objects that your program must explicitly acquire and manipulate. Java provides a cleaner solution. There is no class “Monitor”; instead,each object has its own implicit monitor that is automatically entered when one of the object’s synchronized methods is called.

Messaging


After you divide your program into separate threads, you need to define how they will communicate with each other.Java’s messaging system allows a thread to enter a synchronized method on an object, and then wait there until some other thread explicitly notifies it to come out.

The Thread Class and the Runnable Interface


Java’s multithreading system is built upon the Thread class, its methods, and its companion interface, Runnable , TheThread instance that spawned it. To create a new thread, your program will either extendThread or implement the Runnable interface.

TheThread class defines several methods that help manage threads:

get:
Name Obtain a thread’s name.
get:
Priority Obtain a thread’s priority.
isAlive:
 Determine if a thread is still running.
join :
Wait for a thread to terminate.
run:
 Entry point for the thread.
sleep :
Suspend a thread for a period of time.
start:
 Start a thread by calling its run method.


No comments :

Post a Comment