Notes of V Sem IT JAVA, Java Programming Multithreading.pptx - Study Material
Page 2 :
Multithreading, Multithreading in Java is a process of executing multiple threads simultaneously., A thread is a lightweight sub-process, the smallest unit of processing. , Multiprocessing and multithreading, both are used to achieve multitasking., In Multithreading threads use a shared memory area. , They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process., Java Multithreading is mostly used in games, animation, etc.
Page 3 :
Advantages of Java Multithreading, 1) It doesn't block the user because threads are independent and you can perform multiple operations at the same time., 2) You can perform many operations together, so it saves time., 3) Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread.
Page 4 :
What isThread in java, A thread is a lightweight subprocess, the smallest unit of processing. It is a separate path of execution., Threads are independent. If there occurs exception in one thread, it doesn't affect other threads. It uses a shared memory area.
Page 5 :
Java Thread class, Java provides Thread class to achieve thread programming. Thread class provides constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface.
Page 6 :
Life cycle of a Thread (Thread States), In Java, a thread always exists in any one of the following states. These states are:, New, Active, Blocked / Waiting, Timed Waiting, Terminated
Page 7 :
Explanation of Different Thread States, New: Whenever a new thread is created, it is always in the new state. For a thread in the new state, the code has not been run yet and thus has not begun its execution., Active: When a thread invokes the start() method, it moves from the new state to the active state. The active state contains two states within it: one is runnable, and the other is running., Blocked or Waiting: Whenever a thread is inactive for a span of time (not permanently) then, either the thread is in the blocked state or is in the waiting state.
Page 8 :
Timed Waiting: Sometimes, waiting for leads to starvation. , Terminated: A thread reaches the termination state because of the following reasons:, When a thread has finished its job, then it exists or terminates normally., Abnormal termination: It occurs when some unusual events such as an unhandled exception or segmentation fault.
Page 10 :
Ways of creating Threads, There are two ways to create a thread:, By extending Thread class, By implementing Runnable interface.
Page 11 :
Thread class, Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object class and implements Runnable interface., Commonly used Constructors of Thread class:, Thread(), Thread(String name), Thread(Runnable r), Thread(Runnable r,String name)
Page 12 :
Methods of Thread Class, 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 int getPriority(): returns the priority of the thread., public int setPriority(int priority): changes the priority of the thread., public String getName(): returns the name of the thread., public void setName(String name): changes the name of the thread.
Page 13 :
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., public void suspend(): is used to suspend the thread(depricated)., public void resume(): is used to resume the suspended thread(depricated)., public void stop(): is used to stop the thread(depricated)., public boolean isDaemon(): tests if the thread is a daemon thread., public void setDaemon(boolean b): marks the thread as daemon or user thread., public void interrupt(): interrupts the thread., public boolean isInterrupted(): tests if the thread has been interrupted., public static boolean interrupted(): tests if the current thread has been interrupted.
Page 14 :
Runnable interface, The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run()., public void run(): is used to perform action for a thread.
Page 15 :
Starting a thread, The start() method of Thread class is used to start a newly created thread. It performs the following tasks:, A new thread starts(with new callstack)., The thread moves from New state to the Runnable state., When the thread gets a chance to execute, its target run() method will run.
Page 16 :
Java Thread Example by extending Thread class, FileName: Multi.java, 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(); , } , }
Page 17 :
Using the Thread Class: Thread(Runnable r, String name), public class MyThread2 implements Runnable , { , public void run() , { , System.out.println("Now the thread is running ..."); , } , , // main method , public static void main(String argvs[]) , { , // creating an object of the class MyThread2 , Runnable r1 = new MyThread2(); , , // creating an object of the class Thread using Thread(Runnable r, String name) , Thread th1 = new Thread(r1, "My new thread"); , , // the start() method moves the thread to the active state , th1.start(); , , // getting the thread name by invoking the getName() method , String str = th1.getName(); , System.out.println(str); , } , } , Output:, My new thread Now the thread is running ...
Page 18 :
Synchronization in Java, Synchronization in Java is the capability to control the access of multiple threads to any shared resource., Java Synchronization is better option where we want to allow only one thread to access the shared resource., The synchronization is mainly used to, To prevent thread interference., To prevent consistency problem., Types of Synchronization, There are two types of synchronization, Process Synchronization, Thread Synchronization
Page 19 :
Thread Synchronization, There are two types of thread synchronization mutual exclusive and inter-thread communication., Mutual Exclusive, Synchronized method., Synchronized block., Static synchronization., Cooperation (Inter-thread communication in java)
Page 20 :
Mutual Exclusive, Mutual Exclusive helps keep threads from interfering with one another while sharing data. It can be achieved by using the following three ways:, By Using Synchronized Method, By Using Synchronized Block, By Using Static Synchronization, Concept of Lock in Java, Synchronization is built around an internal entity known as the lock or monitor. Every object has a lock associated with it. By convention, a thread that needs consistent access to an object's fields has to acquire the object's lock before accessing them, and then release the lock when it's done with them.