{{theTime}}

Search This Blog

Total Pageviews

How to add ThreadLocal variables in java


Use ThreadLocal class to provide thread-local variables. ThreadLocal instances are private static fields in classes that wish to associate state with a thread.

For Eg:

 import java.util.concurrent.atomic.AtomicInteger;

 public class UniqueThreadIdGen {

     private static final AtomicInteger id= new AtomicInteger(0);

     private static final ThreadLocal < Integer > uniqueNum = 
         new ThreadLocal < Integer > () {
             @Override protected Integer initialValue() {
                 return id.getAndIncrement();
         }
     };
 
     public static int getCurrentThreadId() {
         return id.get();
     }
 } 

No comments:

Mysql - java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed

Add allowPublicKeyRetrieval=true to the JDBC URL. jdbc:mysql://localhost:3306/db?allowPublicKeyRetrieval=true&useSSL=false