Posts

Showing posts from October, 2022

Synchronization

If multiple threads shares the common area, then data inconsistency will happen. In order to overcome this we need to apply lock. Locking of Method/Block is nothing but synchronization. So that it will allow to execute one thread at a time   Synchronization will be applied to blocks and Methods. If we apply synchronized to a method then at a time only one thread can run that method. It will not allow other threads to execute till the current thread execution is done.  =>This concept is applicable at method and block level. =>if we apply synchronization at block level or at method level then only one thread is allowed to execute the block or a method. => Advantage -> it resolves the problem of "Data Inconsistency/race condition". => Disadvantage->It increase the waiting time for other threads so it affects the performance of the system. Note: In java we have 2 levels of lock a. class level lock => A thread which needs to execute static synchronized blo...

Methods in thread class

 isAlive(): To check whether a thread is alive or not  Can we make a main thread wait? Yes, using the method join(). we can make the main thread wait. join(): To make another thread to wait till it finishes the execution. sleep(): To stop or pause the execution for sometime.

Race condition in Threads

 Threads will be in race condition or in a rush condition in order to better utilize the CPU time cycle. they don't bother about the output, they will rush to finish the task in order to better utilize the CPU time cycle. Due to which data inconsistency will happen.

Demon Thread

 Demon Thread: It is a thread which is of less priority. we can make any thread as demon thread except main thread. by default all  the threads are non-demon. If we make the main thread as demon it will throw illegal exception. Example of Demon thread is Garbage collector. Garbage collector usually cleans the Heap/Unallocated heap area when no one is referring to it. In that case JVM will automatically set it as high priority and cleans the Heap area. If in case if it is referring then JVM will make it as demon thread which is of low priority.

Dead Lock

 Dead Lock: It is a phenomenon where multiple threads are stuck in Blocked state due to mutual dependency of the required resources. ( Lets say there are 2 threads A & B, so what ever resources required by A will be available at B and What ever the resources required by B will be available at A. Both of them have cyclic dependency and Both got stuck in blocked state. Example: class Warrior implements Runnable { String res1=new String("Brhmastra"); String res2=new String("PashuPatastra"); String res3=new String("Sarpastra"); public void run() { try { String s1=Thread.currentThread().getName(); if(s1.equals("Rama")) { ramaAcqRes(); } else { ravanaAcqRes(); } } catch(Exception e ) { System.out.println("Some interruption"); } } public void ramaAcqRes() { try { Thread.sleep(4000); synchronized(res1) { System.out.println("Rama ...

Ways to create a thread and which one is more scalable

 There are 2 ways to create a thread. i) Extending the thread class: ii) Implementing the Runnable interface. As java doesn't support multiple inheritance, So when a class extends Thread class, it cannot extend any other class but when a class implements Runnable interface it can also extend other classes as well. So, Implementing Runnable interface is more Scalable.

States of a Thread

Image
 States of a thread New: When thread is created, it will go to the new state. Runnable: When we call the start() method, then thread will go to the Runnable state, If multiple threads are created and for all the threads if start() method is invoked, then all the threads will go to the Runnable state. Running: Under Running states, there are 3 sub-states. and under Running state at a time only one thread will be present. It depends on the thread scheduler which thread it schedule. i) Sleep: when sleep method is called, then the thread will go to the Sleep state. ii)Blocked: When the requested resources are not available, then the thread will go to the Blocked state. iii)Wait: When wait method is invoked, then the thread will go to the wait state, and again when we call notify() or notifyAll(), then again it will go back to the runnable state  Dead: When the thread life is completed, then it will go to Dead state.