Created
July 29, 2020 17:32
-
-
Save AnjaliManhas/6ca06e2b6e9cc6013ae7001c406ca6fe to your computer and use it in GitHub Desktop.
illustrating the use of Java ThreadLocal initialValue method()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wordpress.compilationerrors; | |
import java.lang.Thread; | |
import java.lang.Runnable; | |
import java.lang.*; | |
// illustrating the use of Java ThreadLocal initialValue() method | |
class NewThread extends Thread { | |
private static ThreadLocal anjali = new ThreadLocal(){ | |
protected Object initialValue(){ | |
return new Integer(a--); | |
} | |
}; | |
private static int a = 15; | |
NewThread(String name) | |
{ | |
super(name); | |
start(); | |
} | |
public void run() | |
{ | |
for (int i = 0; i < 2; i++) | |
System.out.println(getName() + " " + anjali.get()); | |
} | |
} | |
public class ThreadLocalExp { | |
public static void main(String[] args) | |
{ | |
NewThread t1 = new NewThread("compilationErrors1"); | |
NewThread t2 = new NewThread("compilationErrors2"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment