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");
}
}
}
Auf dieser Seite können Sie das Online-Video Multithreading in Java | #53 | StampedLock Class in Java 8 & Higher Versions | A Complete Tutorial mit der Dauer online in guter Qualität ansehen, das der Benutzer my name is GYAN 03 April 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 456 Mal angesehen und es wurde von 8 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!