Java Multi-Threading Question: Download Questions PDF
What is thread Local variable?
![Java Multi-Threading Interview Answer Java Multi-Threading Interview Question](https://globalguideline.com/public/images/interview/33.png)
Answer:
If a variable is declared as threadLocal then all the thread which accesses that variable will maintain its own copy of variable and would not use the copy of any other thread.
For example let consider the scenario of giving JDBC connection to each thread.
public class ThreadLocal {
public Object get();
public void set(Object myValue);
public Object FirstValue();
}
Implementation of ThreadLocal
public class ConnectionDispenser {
private static class ThreadLocalConnection extends ThreadLocal {
public Object FirstValue () {
return DriverManager.getConnection(ConfigurationSingleton.getDbUrl());
}
}
private static ThreadLocalConnection con = new ThreadLocalConnection();
public static Connection MyConnection() {
return (Connection) con.get();
}
}
For example let consider the scenario of giving JDBC connection to each thread.
public class ThreadLocal {
public Object get();
public void set(Object myValue);
public Object FirstValue();
}
Implementation of ThreadLocal
public class ConnectionDispenser {
private static class ThreadLocalConnection extends ThreadLocal {
public Object FirstValue () {
return DriverManager.getConnection(ConfigurationSingleton.getDbUrl());
}
}
private static ThreadLocalConnection con = new ThreadLocalConnection();
public static Connection MyConnection() {
return (Connection) con.get();
}
}
Download Java Multi-Threading Interview Questions And Answers
PDF
Previous Question | Next Question |
What are the methods of the thread class used to schedule the threads? | How to debug our application for issues when multiple threads are being executed? |