Posts

Showing posts with the label Thread

Synchronization in JAVA

Image
You should be aware of Synchronization if you are working on an application where multiple threads are accessing the same resources or else it will show you erroneous or unforeseen results. Imagine a scenario where you are doing the multiple transactions in your bank. I believe you don’t want one thread is updating your balance and in parallel, the other thread is reading your balance otherwise you will end up in error or unexpected results. Example 1: Public class HDFCBank {             Protected long balanace =1000;             Public void deposit( int amount) {                         this.balance = this.balance + amount;             } } Assume if two threads, A and B are executing t...

How to get Current Thread Example

/*         This Java example shows how to get reference of current thread using         currentThread method of Java Thread class. */   public class GetCurrentThread {           public static void main ( String [ ] args ) {                                 /*                  * To get the reference of currently running thread, use                  * Thread currentThread() method of Thread class.                  *                  * This is a static method.                  */                         ...