Java Threads What is Thread Threading is a

  • Slides: 11
Download presentation
Java Threads

Java Threads

What is Thread • Threading is a facility to allow multiple activities to coexist

What is Thread • Threading is a facility to allow multiple activities to coexist within a single process. • Specialized form of Multitasking. • Java is the first mainstream programming language to explicitly include threading within the language itself, rather than treating threading as a facility of the underlying operating system.

What is Thread • A process can support multiple threads, which appear to execute

What is Thread • A process can support multiple threads, which appear to execute simultaneously and asynchronously to each other • Threads are independent, concurrent paths of execution through a program. • Each Thread has – Stack – Program Counter – Local Variables

Thread Sharing • Threads within a process are less insulated from each other than

Thread Sharing • Threads within a process are less insulated from each other than separate processes are. They share memory, file handles. • Does not depend upon Operating System.

Every Java program uses threads • Every Java program has at least one thread

Every Java program uses threads • Every Java program has at least one thread -the main thread. When a Java program starts, the JVM creates the main thread and calls the program's main() method within that thread.

Why use threads • • Make the UI more responsive Take advantage of multiprocessor

Why use threads • • Make the UI more responsive Take advantage of multiprocessor systems Simplify modeling Perform asynchronous or background processing.

Thread States

Thread States

Thread Priority • JVM selects to run a Runnable thread with the highest priority.

Thread Priority • JVM selects to run a Runnable thread with the highest priority. • All Java threads have a priority in the range 1 -10. • Top priority is 10, lowest priority is 1. Normal priority by default is 5. • Thread. MIN_PRIORITY - minimum thread priority • Thread. MAX_PRIORITY - maximum thread priority • Thread. NORM_PRIORITY - maximum thread priority

Thread Scheduling • The JVM schedules using a preemptive , priority based scheduling algorithm.

Thread Scheduling • The JVM schedules using a preemptive , priority based scheduling algorithm. • All Java threads have a priority and the thread with he highest priority is scheduled to run by the JVM. • In case two threads have the same priority a FIFO ordering is followed.

Thread Methods Method Return Type Description current. Thread( ) Thread Returns an object reference

Thread Methods Method Return Type Description current. Thread( ) Thread Returns an object reference to the thread in which it is invoked. get. Name( ) String Retrieve the name of the thread object or instance. start( ) void Start the thread by calling its run method. run( ) void This method is the entry point to execute thread, like the main method for applications. sleep( ) void Suspends a thread for a specified amount of time (in milliseconds).

Thread Methods is. Alive( ) boolean This method is used to determine thread is

Thread Methods is. Alive( ) boolean This method is used to determine thread is running or not. active. Count( ) int This method returns the number of active threads in a particular thread group and all its subgroups. interrupt( ) void The method interrupt the threads on which it is invoked. yield( ) void By invoking this method the current thread pause its execution temporarily and allow other threads to execute. void This method and join(long millisec) Throws Interrupted. Exception. These two methods are invoked on a thread. These are not returned until either the thread has completed or it is timed out respectively. join( )