Skip to content

Instantly share code, notes, and snippets.

@AnjaliManhas
Created July 29, 2020 17:19
Show Gist options
  • Save AnjaliManhas/2e0adb39f197aba17b94888bf2de6e5f to your computer and use it in GitHub Desktop.
Save AnjaliManhas/2e0adb39f197aba17b94888bf2de6e5f to your computer and use it in GitHub Desktop.
illustrating the use of various ThreadLocal methods
package com.wordpress.compilationerrors;
import java.lang.Thread;
import java.lang.Runnable;
import java.lang.*;
// illustrating the use of get(), set(), and remove() ThreadLocal methods
class ThreadLocalExp {
public static void main(String[] args)
{
ThreadLocal<Number> anjaliNumber = new ThreadLocal<Number>();
ThreadLocal<String> anjaliString = new ThreadLocal<String>();
// setting the value
anjaliNumber.set(100);
// returns the current thread's value
System.out.println("value = " + anjaliNumber.get());
// setting the value
anjaliNumber.set(90);
// returns the current thread's value of
System.out.println("value = " + anjaliNumber.get());
// setting the value
anjaliNumber.set(88.45);
// returns the current thread's value of
System.out.println("value = " + anjaliNumber.get());
// setting the value
anjaliString.set("compilationErrors");
// returns the current thread's value of
System.out.println("value = " + anjaliString.get());
// removing value
anjaliString.remove();
// returns the current thread's value of
System.out.println("value = " + anjaliString.get());
// removing vale
anjaliNumber.remove();
// returns the current thread's value of
System.out.println("value = " + anjaliNumber.get());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment