Created
July 29, 2020 17:08
-
-
Save AnjaliManhas/d5015b882d03ec3235f4e80ac07c57c0 to your computer and use it in GitHub Desktop.
implementation of ThreadLocal variable.
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.*; | |
class ThreadLocalExp | |
{ | |
public static class MyRunnable implements Runnable | |
{ | |
private ThreadLocal<Integer> threadLocal = | |
new ThreadLocal<Integer>(); | |
@Override | |
public void run() { | |
threadLocal.set( (int) (Math.random() * 50D) ); | |
try | |
{ | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
} | |
System.out.println(threadLocal.get()); | |
} | |
} | |
public static void main(String[] args) | |
{ | |
MyRunnable runnableInstance = new MyRunnable(); | |
Thread t1 = new Thread(runnableInstance); | |
Thread t2 = new Thread(runnableInstance); | |
// this will call run() method | |
t1.start(); | |
t2.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment