03-03-2025 ------------- Q)What is synchronization? =>Whenever multiple threads are trying to act upon a shared resource, there is a chance of data inconsistency.To prevent such inconsistency, synchronization is used. =>To make a multithreaded application thread-safe, we use synchronization. =>The mechanism of allowing only one thread at a time whenever multiple threads are trying to act upon a shared resource is known as synchronization. =>"synchornized" keyword is used to implement synchronization. =>We have 2 types of synchronization 1)method level synchronization 2)block level synchronization(synchronized statement) =>If we apply synchronized modifier to the method of an object, it is known as method level synchronization. For eg. synchronized void withdraw(){ ...... } =>Instead of declaring the method as synchronized, we can create a block of statements as synchronized. It is known as block level synchronization or synchronized statement. synchronized(object){ //code } Example 1:- public void run(){ synchronized(account){ account.withdraw(); } } Example 2:- public void withdraw(){ //code that doesn’t cause data inconsistency synchronized(this) { //code that casuses data inconsistency } =>when synchronization is applied, the object is locked when a thread accesses the code on that object. Until the lock is released no other thread can access the code on the object. Q)Explain about thread priorities. =>Thread priority is a number used by thread scheduler in allocating CPU cycles to a particular thread in preference to other active threads. =>Thread priority ranges between 1 and 10. =>1 is the least priority, 10 is the maximum priority and 5 is the normal priority. =>java.lang.Thread class has 3 constants 1.)MIN_PRIORITY (1) 2.)NORM_PRIORITY (5) 3.)MAX_PRIORITY(10) =>We can set priority to a Thread by calling setPriority() method on the thread object. For eg. t1.setPriority(Thread.MAX_PRIORITY); OR t1.setPriority(10); =>The higher the priority the more the CPU cycles to a thread. =>By calling getPriority() method on the thread, we can find its priority. =>Priority setting will be effective if and only if it is done before it is activated. =>By default, a thread has a priority of 5. =>main thread has the priority of 5 and therefore all threads created in it aslo have a priority of 5. Q)What is inter-thread communication? =>One thread communicating with another thread by using wait(),notify() and notifyAll() methods is nothing but inter-thread communication. For eg. In Producer-Consumer problem, interthread-communication is implemented. In a synchronized environment only, these 3 methods should be called. Q)Is Java purely object oriented? =>No. =>The moment we have primitive data types in a Programming language, it can't be called as purely object oriented. Note:- Java projects are 100% object oriented. Q)What are wrapper classes? =>In java.lang package, for every primitive data type, one corresponding Java class is given.These classes are known as wrapper classes. primitive type wrapper ************************************ int Integer long Long byte Byte short Short float Float double Double boolean Boolean char Character Note:- An instance of a wrapper class wraps(encapsulates) primitive value in it and hence the name.