Docsity

Thread types and types in java, Lecture notes of Java Programming

Partial preview of the text

Download Thread types and types in java and more Lecture notes Java Programming in PDF only on Docsity!

Thread

 (^) All programmers are familiar with writing sequential programs.  (^) You've probably written a program that displays "Hello World!", or sorts a list of names, or computes a list of prime numbers.  (^) These are sequential programs: each has a beginning, an execution sequence, and an end.  (^) At any given time during the runtime of the program there is a single point of execution.

 (^) Definition: A thread is a single sequential flow of control within a program.  (^) Threads are independent, if there occurs exception in one thread, it doesn't affect other threads. It shares a common memory area.

Creating Thread

There are two ways to create a thread:  (^) By extending Thread class  By implementing Runnable interface.

Methods  (^) public void run(): is used to perform action for a thread.  (^) public void start(): starts the execution of the thread.JVM calls the run() method on the thread.  (^) public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.  (^) public void join(): waits for a thread to die.  (^) public void join(long miliseconds): waits for a thread to die for the specified miliseconds.  (^) public String getName(): returns the name of the thread.  (^) public void setName(String name): changes the name of the thread.  (^) public Thread currentThread(): returns the reference of currently executing thread.

 (^) public int getId(): returns the id of the thread.  (^) public Thread.State getState(): returns the state of the thread.  (^) public boolean isAlive(): tests if the thread is alive.  public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute.

Starting a thread

 (^) start() method of Thread class is used to start a newly created thread. It performs following tasks: A new thread starts  (^) The thread moves from New state to the Runnable state.  (^) When the thread gets a chance to execute, its target run() method will run.

Java Thread Example by extending

Thread class

class Multi extends Thread { public void run(){ System.out.println("thread is running..."); } public static void main(String args[]){ Multi t1=new Multi(); t1.start(); } } This program output : thread is running...

Life cycle of a Thread (Thread States)

 (^) A thread can be in one of the four states.  The life cycle of the thread in java is controlled by JVM.  (^) The java thread states are as follows:  (^) New  (^) Runnable  (^) Non-Runnable (Blocked)  (^) Terminated

 (^) New: The thread is in new state if you create an instance of Thread class but before the invocation of start() method  (^) Runnable The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread  (^) Non-Runnable (Blocked) This is the state when the thread is still alive, but is currently not eligible to run.  (^) Terminated A thread is in terminated or dead state when its run() method exits

Thread Scheduler in Java  (^) Thread scheduler in java is the part of the JVM that decides which thread should run.  (^) There is no guarantee that which runnable thread will be chosen to run by the thread scheduler.  (^) Only one thread at a time can run in a single process.

Sleep method  (^) The sleep() method of Thread class is used to sleep a thread for the specified amount of time  (^) The Thread class provides two methods for sleeping a thread:  (^) public static void sleep(long miliseconds)throws InterruptedException  (^) public static void sleep(long miliseconds, int nanos)throws InterruptedExceptio As you know well that at a time only one thread is executed. If you sleep a thread for the specified time,the thread shedular picks up another thread and so on.

Output: 1 1 2 2 3 3 4 4

join() method

 (^) The join() method waits for a thread to die.  (^) In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.  (^) Methods  (^) Public void join()throws InterruptedException  (^) public void join(long milliseconds)throws InterruptedException