Skip to content

Instantly share code, notes, and snippets.

@AnjaliManhas
Created July 29, 2020 17:08
Show Gist options
  • Save AnjaliManhas/d5015b882d03ec3235f4e80ac07c57c0 to your computer and use it in GitHub Desktop.
Save AnjaliManhas/d5015b882d03ec3235f4e80ac07c57c0 to your computer and use it in GitHub Desktop.
implementation of ThreadLocal variable.
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