Multithreading in Java | #53 | StampedLock Class in Java 8 & Higher Versions | A Complete Tutorial

Published: 03 April 2020
on channel: my name is GYAN
456
8

JAVA & WEB | Session 20 | Multithreading in Java:    • JAVA & WEB | Session 20 | Multithreading a...  

Watch the complete web series "Java & Web" on YouTube. It's complete, comprehensive and conceptual.
   / mynameisgyan  

Follow me on:
Facebook:   / mnisgyan  
Linkedin:   / gyan-prakash-tiwary-07215420  
Twitter:   / mnisgyan  
Website: http://mynameisgyan.website/
Be the patron of this channel:   / mynameisgyan  

#JavaAndWeb #MultithreadingInJava #OCJP #SCJP #javatutorialforbeginners #javainterviewquestionsandanswersforfreshers #javainterviewquestionsandanswers #javaprogramming

CreateThread.java

import java.util.concurrent.locks.*;
class CreateThread
{
public static void main(String[] args)
{
StampedLock lock = new StampedLock();
MyThread[] t = new MyThread[5];
for(int i=0; i lt 5; i++)
{
t[i] = new MyThread("t"+i, lock);
}
for(int i=0; i lt 5; i++)
{
t[i].start();
}
}
}

MyThread.java for writeLock:

import java.util.concurrent.locks.*;
public class MyThread extends Thread
{
StampedLock lock = null;
public MyThread(String name, StampedLock lock)
{
super(name);
this.lock=lock;
}
public void run()
{
long stamp = lock.writeLock();
System.out.println(Thread.currentThread().getName()+" Got the writeLock ");
try
{
Thread.sleep(5000);
}
catch(InterruptedException e){}
try
{
lock.unlockWrite(stamp);
}
catch(IllegalMonitorStateException e)
{
System.out.println(Thread.currentThread().getName()+" "+e+" did not get the lock");
}
finally
{
System.out.println(Thread.currentThread().getName()+" Finished");
}
}
}

MyThread.java (for tryOptimisticRead())

import java.util.concurrent.locks.*;
public class MyThread extends Thread
{
StampedLock lock = null;
public MyThread(String name, StampedLock lock)
{
super(name);
this.lock=lock;
}
public void run()
{
long stamp = 0;
stamp = lock.tryOptimisticRead();
System.out.println(Thread.currentThread().getName()+" Optimistic Read Stamp is "+stamp);
/*Multiple lines of Codes*/
if(!lock.validate(stamp))
{
stamp = lock.readLock();
System.out.println(Thread.currentThread().getName()+" Changed Stamp is "+stamp);
}
try
{
Thread.sleep(5000);
}
catch(InterruptedException e){}
if(lock.isReadLockStamp(stamp))
{
lock.unlockRead(stamp);
System.out.println(Thread.currentThread().getName()+" Unlocking "+stamp);
}
}
}

MyThread.java for lock conversion:

import java.util.concurrent.locks.*;
public class MyThread extends Thread
{
StampedLock lock = null;
public MyThread(String name, StampedLock lock)
{
super(name);
this.lock=lock;
}
public void run()
{
long stamp = lock.readLock();
System.out.println(Thread.currentThread().getName()+" Got the readLock ");
long stamp1 = lock.tryConvertToWriteLock(stamp);
if(stamp1!=0)
{
stamp=stamp1;
}
try
{
lock.unlockWrite(stamp);
}
catch(IllegalMonitorStateException e)
{
System.out.println(Thread.currentThread().getName()+" "+e+" have not unlocked");
}
finally
{
System.out.println(Thread.currentThread().getName()+" Finished");
}
}
}


On this page of the site you can watch the video online Multithreading in Java | #53 | StampedLock Class in Java 8 & Higher Versions | A Complete Tutorial with a duration of online in good quality, which was uploaded by the user my name is GYAN 03 April 2020, share the link with friends and acquaintances, this video has already been watched 456 times on youtube and it was liked by 8 viewers. Enjoy your viewing!