Java.lang.ThreadLocal.get() Method


Advertisements


Description

The java.lang.ThreadLocal.get() method returns the value in the current thread's copy of this thread-local variable.

Declaration

Following is the declaration for java.lang.ThreadLocal.get() method

public T get()

Parameters

NA

Return Value

This method returns the current thread's value of this thread-local.

Exception

NA

Example

The following example shows the usage of java.lang.ThreadLocal.get() method.

package com.tutorialspoint;

import java.lang.*;

public class ThreadLocalDemo {

   public static void main(String[] args) {

      ThreadLocal<Integer> tlocal = new ThreadLocal<Integer>();  

      tlocal.set(100);
      // returns the current thread's value
      System.out.println("value = " + tlocal.get());
 
      tlocal.set(90);
      // returns the current thread's value of 
      System.out.println("value = " + tlocal.get());
   }
} 

Let us compile and run the above program, this will produce the following result −

value = 100
value = 90

java_lang_threadlocal.htm

Advertisements