What is thread Local variable?
Submitted by: AdministratorIf 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();
}
}
Submitted by: Administrator
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();
}
}
Submitted by: Administrator
Read Online Java Multi-Threading Job Interview Questions And Answers
Top Java Multi-Threading Questions
☺ | Explain deadlock? |
☺ | What is the sleep() method Data type for the parameter? |
☺ | Explain the difference between preemptive scheduling and time slicing? |
☺ | How many threads at a time can access a monitor? |
☺ | Name the default thread at the time of starting the program? |
Top Java Programming Language Categories
☺ | Core Java Interview Questions. |
☺ | Hibernate Interview Questions. |
☺ | Advanced Java Interview Questions. |
☺ | IBM WebSphere Interview Questions. |
☺ | Spring Framework Interview Questions. |