Sunday, August 23, 2015

Thread Basics - I

Thread: Why to use thread?
  • To run task parallel and concurrently
  • To utilise the hardware resource efficiently multi core/mullti cpu+ core  processor

Thread name :
-Is mutable
-Multiple thread can share same name.

Volatile: When to use volatile?

shared variable: one thread read/write + multiple thread read .
Will not work for : thread 1 read/write + thread2 read//write 

Synchronization:
thread 1 read/write + thread2 read/write 

Thread Priorities:
  • Each java threads open a new native thread at OS level. Setting priorities translates to native priorities based on the OS platform
  • Not recommended to rely solely on priorities
Thread Local Storage:
-Exist as long as the thread is alive

public static class PaymentResponseData
{
public int transactionId;
public String transactionStatus;

public int userId;
}

public static final ThreadLocal<PaymentResponseData> globalData =
new ThreadLocal<PaymentResponseData>();

globalData.set(tranacationData) 
 globalData.get().

 myThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

   public void uncaughtException(Thread t, Throwable e) {
   PaymentResponseData paymentResponseData=  globalData.get()

System.err.println("Error occurred with transaction ID " + paymentResponseData.get().transactionId);   }
   });

Demon thread:
Provide service to the user thread. Eg gc
Collecting statistics and performing the status monitoring tasks 

No comments:

Post a Comment