Threads

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Threads as PDF for free.

More details

  • Words: 691
  • Pages: 14
Threads - Overview

What is a thread ?  





single flow of control is a kind of a “lightweight process” every Java program consists of threads application programmers can create and manipulate threads

Basics 





In the JVM, there is a main memory, which is shared between all threads, and each thread has its own private working memory, no thread can access other threads working memory. Threads take their turns in performing instructions by sharing the CPU time between them. How and when one of the threads is scheduled (or descheduled) for execution is dictated by the scheduler, which depends on the operating system  

Preemptive(win3.x, early Mac OS) Co-operative(winNT etc.)

Basics … 





The first rule of using threads is: avoid them when you can !! Basic support for threads in all versions of the Java platform is in the java.lang.Thread class Generic behaviors provided by thread class include     



starting, sleeping, running, yielding, and having a priority

To implement a thread using the Thread class, you need to provide it with a run method that performs the thread’s task.

Basics …  





Each object has a lock associated with it. Critical sections of the code that access a common resource are guarded by monitors (which essentially are synchronized methods or blocks). To enter a monitor a thread has to acquire the lock corresponding to the monitor. One and only one thread can acquire lock of an object and thus enter the critical section at any point of time.

Thread model

Using ‘Thread’ 

There are two options for providing a run method for a thread:  



Sub classing ‘Thread’ and overriding “run” Implementing the ‘runnable’ interface

Rule of Thumb:  If your class must subclass some other class (the most common example being Applet), you should use ‘runnable’ as described in option

Thread States

New Thread State  







EgThread = new Thread(this, “Example"); After this statement has been executed, EgThread is in the New Thread state. When a thread is a New Thread, it is merely an empty Thread object; no system resources have been allocated for it yet. When a thread is in this state, you can only start the thread. Calling any method besides start when a thread is in this state makes no sense and causes an IllegalThreadStateException

Starting a thread  

EgThread.start(); The start method 

 





creates the system resources necessary to run the thread, schedules the thread to run, and calls the thread's run method.

After the start method has returned, the thread is "running". So at any given time, a "running" thread actually may be waiting for its turn in the CPU.

Runnable/Not Runnable 

A thread becomes Not Runnable when one of these events occurs:  









Its sleep method is invoked. The thread calls the wait method to wait for a specific condition to be satisfied. The thread is blocking on I/O.

If a thread has been put to sleep, then the specified number of milliseconds must elapse. If a thread is waiting for a condition, then another object must notify the waiting thread of a change in condition by calling notify or notifyAll. If a thread is blocked on I/O, then the I/O must complete.

isAlive ? 









The API for the Thread class includes a method called isAlive. The isAlive method returns true if the thread has been started and not stopped. If the isAlive method returns false, you know that the thread either is a New Thread or is Dead. If the isAlive method returns true, you know that the thread is either Runnable or Not Runnable. You cannot differentiate between a New Thread or a Dead thread. Nor can you differentiate between a Runnable thread and a

Deprecated Methods    

countStackFrames() Resume() Stop() Suspend()



Why is Thread.stop deprecated?  

Because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked

Related Documents

Threads
December 2019 37
Threads
May 2020 26
Threads
April 2020 15
Threads
June 2020 13
Threads
October 2019 28
Threads
July 2020 18