Open zhamri opened 4 years ago
import java.util.concurrent.locks.ReentrantLock;
public class MyjavaInter {
static int number1=0;
public static ReentrantLock lock = new ReentrantLock();
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
// TODO code application logic here
Thread t1 = new threadA();
Thread t2 = new threadB();
Thread t3 = new threadC();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
static class threadA extends Thread{
public void run(){
for (int number = 1; number<=10 ; number++){
if(number==5 || number ==6||number ==7) {
lock.lock();
System.out.println("Thread A Lock : "+number);
lock.unlock();
}else{
System.out.println("Thread A : "+number);
}
}
}
}
static class threadB extends Thread{
public void run(){
for (int number = 1; number<=10 ; number++){
if(number==5 || number ==6||number ==7) {
lock.lock();
System.out.println("Thread B Lock : "+number);
lock.unlock();
}else{
System.out.println("Thread B : "+number);
}
}}
}
static class threadC extends Thread{
public void run(){
for (int number = 1; number<=10 ; number++){
if(number==5 || number ==6||number ==7) {
lock.lock();
System.out.println("Thread C Lock : "+number);
lock.unlock();
}else{
System.out.println("Thread C : "+number);
}}
}
}
}
Youtube Links : https://www.youtube.com/watch?v=i7cnZRVbxkE
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Thread06;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* @author WANG LUO
*/
public class LockTest {
public static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) throws InterruptedException{
Thread a = new Thread01();
a.setName("A");
Thread b = new Thread02();
b.setName("B");
Thread c = new Thread03();
c.setName("C");
a.start();
a.join();
b.start();
b.join();
c.start();
c.join();
}
static class Thread01 extends Thread{
public void run(){
for(int i = 1; i <= 10; i++){
switch(i){
case 5:
lock.lock();
System.out.print("Lock: ");
lock.unlock();
break;
case 6:
lock.lock();
System.out.print("Lock: ");
lock.unlock();
break;
case 7:
lock.lock();
System.out.print("Lock: ");
lock.unlock();
break;
}
System.out.println(i);
}
}
}
static class Thread02 extends Thread{
public void run(){
for(int i = 1; i <= 10; i++){
switch(i){
case 5:
lock.lock();
System.out.print("Lock: ");
lock.unlock();
break;
case 6:
lock.lock();
System.out.print("Lock: ");
lock.unlock();
break;
case 7:
lock.lock();
System.out.print("Lock: ");
lock.unlock();
break;
}
System.out.println(i);
}
}
}
static class Thread03 extends Thread{
public void run(){
for(int i = 1; i <= 10; i++){
switch(i){
case 5:
lock.lock();
System.out.print("Lock: ");
lock.unlock();
break;
case 6:
lock.lock();
System.out.print("Lock: ");
lock.unlock();
break;
case 7:
lock.lock();
System.out.print("Lock: ");
lock.unlock();
break;
}
System.out.println(i);
}
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Week_07;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* @author MICKY
*/
class Thread1 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
lock.lock();
try {
for (int i = 1; i <= 10; i++) {
switch(i){
case 5:
System.out.print("Thread1 Lock:");
break;
case 6:
System.out.print("Thread1 Lock:");
break;
case 7:
System.out.print("Thread1 Lock:");
break;
}
System.out.println(i);
}
} catch (Exception e) {
lock.unlock();
e.printStackTrace();
}
}
}
class Thread2 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
lock.lock();
try {
for (int i = 1; i <= 10; i++) {
switch(i){
case 5:
System.out.print("Thread2 Lock:");
break;
case 6:
System.out.print("Thread2 Lock:");
break;
case 7:
System.out.print("Thread2 Lock:");
break;
}
System.out.println(i);
}
} catch (Exception e) {
lock.unlock();
e.printStackTrace();
}
}
}
class Thread3 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
lock.lock();
try {
for (int i = 1; i <= 10; i++) {
switch(i){
case 5:
System.out.print("Thread3 Lock:");
break;
case 6:
System.out.print("Thread3 Lock:");
break;
case 7:
System.out.print("Thread3 Lock:");
break;
}
System.out.println(i);
}
} catch (Exception e) {
lock.unlock();
e.printStackTrace();
}
}
}
public class issueNine {
public static void main(String[] args) {
Thread1 t1 = new Thread1();
Thread2 t2= new Thread2();
Thread3 t3= new Thread3();
t1.start();
t2.start();
t3.start();
}
}
import java.util.concurrent.locks.ReentrantLock;
public class TestLock
{
public static ReentrantLock lock = new ReentrantLock();
public static void main (String [] args) throws Exception
{
Thread t1 = new Thread(new Runnable() {
@Override
public void run()
{
for(int i=2;i<10;i++)
{
if(i==5 || i==6 || i==7)
{
lock.lock();
System.out.println("Thread 1 Lock : "+i);
lock.unlock();
}
else
{
System.out.println("Thread 1 : "+i);
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public synchronized void run() {
for(int i=2;i<10;i++)
{
if(i==5 || i==6 || i==7)
{
lock.lock();
System.out.println("Thread 2 Lock : "+i);
lock.unlock();
}
else
{
System.out.println("Thread 2 : "+i);
}
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public synchronized void run() {
for(int i=2;i<10;i++)
{
if(i==5 || i==6 || i==7)
{
lock.lock();
System.out.println("Thread 3 Lock : "+i);
lock.unlock();
}
else
{
System.out.println("Thread 3 : "+i);
}
}
}
});
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
}
Tan Cheng Yi 253814
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
public static void main(String[] args) {
// write your code here
Thread t1=new count("Thread 1");
Thread t2=new count("Thread 2");
Thread t3=new count("Thread 3");
t1.start();
t2.start();
t3.start();
}
}
class count extends Thread{
Lock lock = new ReentrantLock();
count(String name){
super(name);
}
@Override
public void run() {
for(int i=1;i<=10;i++){
if(i==5|i==6|i==7) {
lock.lock();
System.out.println("Locked");
System.out.println(getName() + "=" + i);
lock.unlock();
System.out.println("Unlocked");
}
else {
System.out.println(getName()+"="+i);
}
}
}
}
import java.util.concurrent.locks.ReentrantLock;
public class Issue {
private static ReentrantLock lock = new ReentrantLock();
private static void accessResource() {
for (int i = 1; i <= 10; i++) {
switch (i) {
case 5:
lock.lock();
{
for (int j = 5; j <= 7; j++) {
System.out.println(Thread.currentThread().getName() + "-" + j + " lock");
}
}
i = 8;
lock.unlock();
break;
default:
System.out.println(Thread.currentThread().getName() + "-" + i);
}
}
}
public static void main(String[] args) {
Thread t1 = new Thread(() -> accessResource(), "Thread 1");
t1.start();
Thread t2 = new Thread(() -> accessResource(), "Thread 2");
t2.start();
Thread t3 = new Thread(() -> accessResource(), "Thread 3");
t3.start();
}
}
youtube video link:
https://www.youtube.com/watch?v=ahBC69_iyk4
import java.util.concurrent.locks.ReentrantLock;
public class exercise {
public static void main(String[] args) throws InterruptedException {
final ReentrantLock rlock = new ReentrantLock(true);
Thread threadOne = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
if (i == 5 || i == 6 || i == 7) {
rlock.lock();
try {
System.out.println("Thread 1 lock: " + i);
} finally {
rlock.unlock();
}
} else {
System.out.println("Thread 1: " + i);
}
}
}
});
Thread threadTwo = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
if (i == 5 || i == 6 || i == 7) {
rlock.lock();
try {
System.out.println("Thread 2 lock: " + i);
} finally {
rlock.unlock();
}
} else {
System.out.println("Thread 2: " + i);
}
}
}
});
Thread threadThree = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
if (i == 5 || i == 6 || i == 7) {
rlock.lock();
try {
System.out.println("Thread 3 lock: " + i);
} finally {
rlock.unlock();
}
} else {
System.out.println("Thread 3: " + i);
}
}
}
});
threadOne.start();
threadOne.join();
threadTwo.start();
threadTwo.join();
threadThree.start();
threadThree.join();
}
}
youtube link : https://www.youtube.com/watch?v=fjMTaVykOpc
import java.util.concurrent.locks.ReentrantLock;
public class test {
public static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) throws InterruptedException {
MyThread();
}
public static void MyThread() throws InterruptedException {
Thread t1 = new Thread() {
public void run() {
for (int i = 1; i <= 10; i++) {
synchronized (this) {
switch (i) {
case 5:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
case 6:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
case 7:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
}
}
System.out.println("The current number is:" + i);
}
}
};
Thread t2 = new Thread() {
public void run() {
for (int i = 1; i <= 10; i++) {
synchronized (this) {
switch (i) {
case 5:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
case 6:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
case 7:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
}
}
System.out.println("The current number is:" + i);
}
}
};
Thread t3 = new Thread() {
public void run() {
for (int i = 1; i <= 10; i++) {
synchronized (this) {
switch (i) {
case 5:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
case 6:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
case 7:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
}
}
System.out.println("The current number is:" + i);
}
}
};
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
}
Source Code
import java.util.concurrent.locks.ReentrantLock;
public class mainFlow {
private static ReentrantLock reentrantLock = new ReentrantLock();
private static final String ANSI_PURPLE = "\u001B[35m";
private static final String ANSI_RESET = "\u001B[0m";
public static void main(String[] args) {
Thread thread1 = new Thread(mainFlow::accessResource, "Thread-1");
Thread thread2 = new Thread(mainFlow::accessResource, "Thread-2");
Thread thread3 = new Thread(mainFlow::accessResource, "Thread-3");
thread1.start();
thread2.start();
thread3.start();
}
private static void accessResource() {
int i;
for (i = 1; i < 10; i++) {
if (i == 5) {
reentrantLock.lock();
int j;
for (j = 5; j < 8; j++) {
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
reentrantLock.isLocked();
ie.printStackTrace();
}
System.out.println(ANSI_PURPLE + "Locked: " +
Thread.currentThread().getName() + ":" + j + ANSI_RESET);
}
reentrantLock.unlock();
}
else if (i <= 4 || i >= 8) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}
Expected Output
Youtube Links
Video Title | Link |
---|---|
Java ReentrantLock - Fairness & TryLock | https://www.youtube.com/watch?v=ahBC69_iyk4 |
ReentrantLock Concurrency APIs JAVA Example | https://www.youtube.com/watch?v=lXCz4GcJnoE |
Relock Class
package com.baktajivan;
public class Relock extends Thread{
//implementing lock and reentrant lock in these thread
public static void main(String [] args){
Thread1 T1 = new Thread1();
Thread2 T2 = new Thread2();
Thread3 T3 = new Thread3();
T1.start();
T2.start();
T3.start();
}
}//end class
Thread1 Class
package com.baktajivan;
//imports
import java.util.concurrent.locks.ReentrantLock;
public class Thread1 extends Thread{
public void run(){
ReentrantLock relock = new ReentrantLock();
try{
for (int i = 1;i<=10; i++){
if(i == 5){
relock.lock();
for (i = 5; i<8; i++){
System.out.println("Thread 1 is Lock: "+i);
//prints is thread is lock
}//for print 5-7
relock.unlock();
System.out.println("Thread 1 is UnLock");
}//if
else if(i <= 4 || i >= 8){
System.out.println("Thread 1 : "+i);
//prints as usual [should interleave]
}//else
}//for
}finally {
System.out.println("Thread 1 is Done");
//prints is unlock a thread
}//finally
}
}
Thread2 Class
package com.baktajivan;
//imports
import java.util.concurrent.locks.ReentrantLock;
public class Thread2 extends Thread{
public void run(){
ReentrantLock relock = new ReentrantLock();
try{
for (int i = 1;i<=10; i++){
if(i == 5){
sleep(100);
relock.lock();
for (i = 5; i<8; i++){
System.out.println("Thread 2 is Lock: "+i);
//prints is thread is lock
}//for print 5-7
relock.unlock();
System.out.println("Thread 2 is UnLock");
}//if
else if(i <= 4 || i >= 8){
System.out.println("Thread 2 : "+i);
//prints as usual [should interleave]
}//else
}//for
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("Thread 2 is Done");
//prints is unlock a thread
}//finally
}
}
Thread3 Class
package com.baktajivan;
//imports
import java.util.concurrent.locks.ReentrantLock;
public class Thread3 extends Thread{
public void run(){
ReentrantLock relock = new ReentrantLock();
try{
for (int i = 1;i<=10; i++){
if(i == 5){
sleep(200);
relock.lock();
for (i = 5; i<8; i++){
System.out.println("Thread 3 is Lock: "+i);
//prints is thread is lock
}//for print 5-7
relock.unlock();
System.out.println("Thread 3 is UnLock");
}//if
else if(i <= 4 || i >= 8){
System.out.println("Thread 3 : "+i);
//prints as usual [should interleave]
}//else
}//for
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("Thread 3 is Done");
//prints is unlock a thread
}//finally
}
}
Output:
Youtube Reference Link:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
class lockclass implements Runnable
{
String name;
ReentrantLock re;
public lockclass(ReentrantLock rl, String n)
{
re = rl;
name = n;
}
public void run()
{
boolean done = false;
while (!done)
{
//Getting Outer Lock
boolean ans = re.tryLock();
// Returns True if lock is free
if(ans)
{
try
{
Date d = new Date();
SimpleDateFormat ft = new SimpleDateFormat("hh:mm:ss");
System.out.println("task name - "+ name
+ " outer lock acquired at "
+ ft.format(d)
+ " Doing outer work");
Thread.sleep(1500);
// Getting Inner Lock
re.lock();
try
{
d = new Date();
ft = new SimpleDateFormat("hh:mm:ss");
System.out.println("task name - "+ name
+ " inner lock acquired at "
+ ft.format(d)
+ " Doing inner work");
System.out.println("Lock Hold Count - "+ re.getHoldCount());
Thread.sleep(1500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
//Inner lock release
System.out.println("task name - " + name +
" releasing inner lock");
re.unlock();
}
System.out.println("Lock Hold Count - " + re.getHoldCount());
System.out.println("task name - " + name + " work done");
done = true;
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
//Outer lock release
System.out.println("task name - " + name +
" releasing outer lock");
re.unlock();
System.out.println("Lock Hold Count - " +
re.getHoldCount());
}
}
else
{
System.out.println("task name - " + name +
" waiting for lock");
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
------------------------------------------------------------------------
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
public class locktwo
{
static final int MAX_T = 2;
public static void main(String[] args)
{
ReentrantLock rel = new ReentrantLock();
ExecutorService pool = Executors.newFixedThreadPool(MAX_T);
Runnable w1 = new lockclass(rel, "Job1");
Runnable w2 = new lockclass(rel, "Job2");
Runnable w3 = new lockclass(rel, "Job3");
Runnable w4 = new lockclass(rel, "Job4");
pool.execute(w1);
pool.execute(w2);
pool.execute(w3);
pool.execute(w4);
pool.shutdown();
}
}
https://www.youtube.com/watch?v=i7cnZRVbxkE&t=255s https://www.youtube.com/watch?v=Qy81TtAkmQw&t=104s https://www.youtube.com/watch?v=fjMTaVykOpc&t=486s
import java.util.concurrent.locks.ReentrantLock;
/*
public class Exe_lock {
public static void main(String[] args) throws InterruptedException {
final ReentrantLock rlock = new ReentrantLock(true);
Thread threadOne = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
if (i == 5 || i == 6 || i == 7) {
rlock.lock();
try {
System.out.println("Thread 1 lock: " + i);
} finally {
rlock.unlock();
}
} else {
System.out.println("Thread 1: " + i);
}
}
}
});
Thread threadTwo = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
if (i == 5 || i == 6 || i == 7) {
rlock.lock();
try {
System.out.println("Thread 2 lock: " + i);
} finally {
rlock.unlock();
}
} else {
System.out.println("Thread 2: " + i);
}
}
}
});
Thread threadThree = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
if (i == 5 || i == 6 || i == 7) {
rlock.lock();
try {
System.out.println(" Thread 3 lock: " + i);
} finally {
rlock.unlock();
}
} else {
System.out.println("Thread 3: " + i);
}
}
}
});
threadOne.start();
threadOne.join();
threadTwo.start();
threadTwo.join();
threadThree.start();
threadThree.join();
}
}
Youtube Reference Link:
https://www.youtube.com/watch?v=fjMTaVykOpc
TAN WEI DI 259296
import java.util.concurrent.locks.ReentrantLock;
public class Lock extends Thread {
private static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) {
Lock thread1 = new Lock();
Lock thread2 = new Lock();
Lock thread3 = new Lock();
thread1.setName("thread1");
thread2.setName("thread2");
thread3.setName("thread3");
thread1.start();
thread2.start();
thread3.start();
}
public void run(){
for(int i=1;i<10;i++) {
if(i==5) {
lock.lock(); // block until condition holds
try {
while (i!=8) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("\u001B[33mLock-" + Thread.currentThread().getName() + " : " + i + "\u001B[0m");
i++;
}// ... method body
} finally {
lock.unlock();
}
}
System.out.println(Thread.currentThread().getName() + " : " + i);
}
}
}
import java.util.concurrent.locks.ReentrantLock;
public class Lock { public static ReentrantLock lockNum = new ReentrantLock();
public static void main(String[] args) throws InterruptedException
{
Thread t1 = new thread1();
Thread t2 = new thread2();
Thread t3 = new thread3();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
public static class thread1 extends Thread
{
public void run()
{
for (int num = 1; num<=10 ; num++)
{
if(num>=5 && num<=7)
{
lockNum.lock();
System.out.println("Thread 1 Lock : "+num);
lockNum.unlock();
}else
{
System.out.println("Thread 1 : "+num);
}
}
}
}
public static class thread2 extends Thread
{
public void run()
{
for (int num = 1; num<=10 ; num++)
{
if(num>=5 && num<=7)
{
lockNum.lock();
System.out.println("Thread 2 Lock : "+num);
lockNum.unlock();
}else
{
System.out.println("Thread 2 : "+num);
}
}
}
}
public static class thread3 extends Thread
{
public void run()
{
for (int num = 1; num<=10 ; num++)
{
if(num>=5 && num<=7)
{
lockNum.lock();
System.out.println("Thread 3 Lock : "+num);
lockNum.unlock();
}else
{
System.out.println("Thread 3 : "+num);
}
}
}
}
}
YoutubeLink https://www.youtube.com/watch?v=N0mMm5PF5Ow
import java.util.concurrent.locks.ReentrantLock;
public class threadClass {
public static ReentrantLock lock = new ReentrantLock();
public static void main(String [] args) throws InterruptedException{
Thread();
}
public static void Thread() throws InterruptedException{
Thread T1 = new Thread(){
public void run(){
for (int i = 1;i<=10; i++){
switch (i)
{
case 5:
case 7:
case 6:
lock.lock();
System.out.print("protect : ");
lock.unlock();
break;
}
System.out.println(" " + i);
}
}
};
Thread T2 = new Thread(){
public void run(){
for (int i = 1;i<=10; i++){
switch (i)
{
case 5:
case 7:
case 6:
lock.lock();
System.out.print("protect : ");
lock.unlock();
break;
}
System.out.println(" " + i);
}
}
};
Thread T3 = new Thread(){
public void run(){
for (int i = 1;i<=10; i++){
switch (i)
{
case 5:
case 7:
case 6:
lock.lock();
System.out.print("protect : ");
lock.unlock();
break;
}
System.out.println(" " + i);
}
}
};
T1.start();
T1.join();
T2.start();
T2.join();
T3.start();
T3.join();
}
}
muhammad sufyan bin ahmad kamal 255541
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;
public class exercise {
public static void main(String[] args) {
// write your code here
Thread t1=new count("thread 1");
Thread t2=new count("thread 2");
Thread t3=new count("thread 3");
t1.start();
t2.start();
t3.start();
}
}
class count extends Thread{ Lock lock = new ReentrantLock(); count(String name){ super(name); } @Override public void run() { for(int i=1;i<=10;i++){ if(i==5|i==6|i==7) { lock.lock(); try { System.out.println("locked"); System.out.println(getName() + "=" + i); } finally { lock.unlock(); } System.out.println("unlocked"); } else { System.out.println(getName()+"="+i); } }
}
}
import java.util.concurrent.locks.ReentrantLock;
class Thread1 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
lock.lock();
try {
for (int x = 1; x <= 10; x++) {
switch(x){
case 5:
System.out.print("Lock number ");
break;
case 6:
System.out.print("Lock number ");
break;
case 7:
System.out.print("Lock number ");
break;
}
System.out.println(x);
}
} catch (Exception e) {
lock.unlock();
e.printStackTrace();
}
}
}
class Thread2 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
lock.lock();
try {
for (int x = 1; x <= 10; x++) {
switch(x){
case 5:
System.out.print("Lock number ");
break;
case 6:
System.out.print("Lock number ");
break;
case 7:
System.out.print("Lock number ");
break;
}
System.out.println(x);
}
} catch (Exception e) {
lock.unlock();
e.printStackTrace();
}
}
}
class Thread3 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
lock.lock();
try {
for (int x = 1; x <= 10; x++) {
switch(x){
case 5:
System.out.print("Lock number ");
break;
case 6:
System.out.print("Lock number ");
break;
case 7:
System.out.print("Lock number ");
break;
}
System.out.println(x);
}
} catch (Exception e) {
lock.unlock();
e.printStackTrace();
}
}
}
class Main {
public static void main(String[] args){
Thread1 t1 = new Thread1();
Thread2 t2= new Thread2();
Thread3 t3= new Thread3();
t1.start();
t2.start();
t3.start();
}
}
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Count {
public static void main(String args[]) throws InterruptedException {
Thread t1 =new read("Thread 1");
Thread t2 =new read("Thread 2");
Thread t3 =new read("Thread 3");
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
}
class read extends Thread {
Lock lock = new ReentrantLock();
read(String name) {
super(name);
}
@Override
public void run() {
synchronized (this) {
for (int i = 1; i <= 10; i++) {
lock.lock();
switch (i) {
case 5:
case 7:
case 6:
System.out.print("Lock Number: ");
break;
}
System.out.println("Current Number : " + i);
}
}
}
}
package Issue9;
import java.util.concurrent.locks.ReentrantLock;
public class Issue9 {
private static Thread tone = new thread_1();
private static Thread ttwo = new thread_2();
private static Thread tthree = new thread_3();
public static void main(String[] args) {
tone.setName("tone");
ttwo.setName("ttwo");
tthree.setName("tthree");
tone.start();
ttwo.start();
tthree.start();
}
}
public class thread_1 extends Thread{
private static final String PURPLE = "\u001B[36m";
private static final String BLANK = "\u001B[0m";
private static int tNo1 = 1;
private static ReentrantLock lk = new ReentrantLock();
public void run() {
for (; tNo1 <= 10; tNo1++) {
if (tNo1 == 5) {
lk.lock();
try {
int i = tNo1;
for (; i <= 7; i++) {
System.out.println(PURPLE + "Protect1: " + i + BLANK);
}
} finally {
lk.unlock();
}
}
if (tNo1 != 5 && tNo1 != 6 && tNo1 != 7) {
System.out.println("T1: " + tNo1);
}
}
}
}
public class thread_2 extends Thread{
private static final String PURPLE = "\u001B[36m";
private static final String BLANK = "\u001B[0m";
private static int tNo2 = 1;
private static ReentrantLock lk = new ReentrantLock();
public void run() {
for (; tNo2 <= 10; tNo2++) {
if (tNo2 == 5) {
lk.lock();
try {
int i = tNo2;
for (; i <= 7; i++) {
System.out.println(PURPLE + "Protect2: " + i + BLANK);
}
} finally {
lk.unlock();
}
}
if (tNo2 != 5 && tNo2 != 6 && tNo2 != 7) {
System.out.println("T2: " + tNo2);
}
}
}
}
public class thread_3 extends Thread{
private static final String PURPLE = "\u001B[36m";
private static final String BLANK = "\u001B[0m";
private static int tNo3 = 1;
private static ReentrantLock lk = new ReentrantLock();
public void run() {
for (; tNo3 <= 10; tNo3++) {
if (tNo3 == 5) {
lk.lock();
try {
int i = tNo3;
for (; i <= 7; i++) {
System.out.println(PURPLE + "Protect3: " + i + BLANK);
}
} finally {
lk.unlock();
}
}
if (tNo3 != 5 && tNo3 != 6 && tNo3 != 7) {
System.out.println("T3: " + tNo3);
}
}
}
}
// References: Defog Tech. (2018, Sep 24). Java ReentrantLock - fairness, tryLock and more [Video file]. Retrieved from https://www.youtube.com/watch?v=ahBC69_iyk4
package com.company;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Locking extends Thread{
private final Lock lock = new ReentrantLock();
public void run(){
for (int i = 1; i <= 10; i++) {
try{
lock.lock();
if(i==5 || i==6 || i==7){ System.out.println(Thread.currentThread().getName()+": " +i+ " is protected.");}
else { System.out.println(Thread.currentThread().getName()+": " +i);}
}finally {
lock.unlock();
}
}
}
public static void main(String[] args){
Locking l = new Locking( );
Thread t1 = new Thread(l);
Thread t2 = new Thread(l);
Thread t3 = new Thread(l);
t1.start();
t2.start();
t3.start();
}
}
Youtube link :
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
static class thread extends Thread {
Lock lock = new ReentrantLock();
thread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
if (i == 5 | i == 6 | i == 7) {
lock.lock();
System.out.println(getName() + "=" + i + " Locked");
lock.unlock();
} else {
System.out.println(getName() + "=" + i);
}
}
}
}
public static void main(String[] args) {
Thread t1=new thread("Thread 1");
Thread t2=new thread("Thread 2");
Thread t3=new thread("Thread 3");
t1.start();
t2.start();
t3.start();
}
}
package com.company.shyhhaw;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
static final int MAX_T = 2;
public static void main(String[] args){
ReentrantLock rel = new ReentrantLock();
ExecutorService pool = Executors.newFixedThreadPool(MAX_T);
Runnable w1 = new LockClass(rel, "Job1");
Runnable w2 = new LockClass(rel, "Job2");
Runnable w3 = new LockClass(rel, "Job3");
Runnable w4 = new LockClass(rel, "Job4");
pool.execute(w1);
pool.execute(w2);
pool.execute(w3);
pool.execute(w4);
pool.shutdown();
}
}
---------------------------------------------------------------------------------------------------------------
package com.company.shyhhaw;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
class LockClass implements Runnable
{
String name;
ReentrantLock re;
public LockClass(ReentrantLock rl, String n)
{
re = rl;
name = n;
}
public void run()
{
boolean done = false;
while (!done)
{
//Getting Outer Lock
boolean ans = re.tryLock();
// Returns True if lock is free
if(ans)
{
try
{
Date d = new Date();
SimpleDateFormat ft = new SimpleDateFormat("hh:mm:ss");
System.out.println("task name - "+ name
+ " outer lock acquired at "
+ ft.format(d)
+ " Doing outer work");
Thread.sleep(1500);
// Getting Inner Lock
re.lock();
try
{
d = new Date();
ft = new SimpleDateFormat("hh:mm:ss");
System.out.println("task name - "+ name
+ " inner lock acquired at "
+ ft.format(d)
+ " Doing inner work");
System.out.println("Lock Hold Count - "+ re.getHoldCount());
Thread.sleep(1500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
//Inner lock release
System.out.println("task name - " + name +
" releasing inner lock");
re.unlock();
}
System.out.println("Lock Hold Count - " + re.getHoldCount());
System.out.println("task name - " + name + " work done");
done = true;
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
//Outer lock release
System.out.println("task name - " + name +
" releasing outer lock");
re.unlock();
System.out.println("Lock Hold Count - " +
re.getHoldCount());
}
}
else
{
System.out.println("task name - " + name +
" waiting for lock");
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
-----------------
- Youtube Link -
-----------------
https://www.youtube.com/watch?v=UrEmEOhFqwk
-----------------
- HTML Link -
-----------------
package real_time_programming;
import java.util.concurrent.locks.ReentrantLock;
public class Issue_9 {
public static void main(String[] args) throws InterruptedException {
Threadd thd1 = new Threadd();
Threadd thd2 = new Threadd();
Threadd thd3 = new Threadd();
thd1.start();
thd1.join();
thd2.start();
thd2.join();
thd3.start();
thd3.join();
}
}
class Threadd extends Thread{
private static ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
synchronized (this){
for(int i = 1;i <= 10;i++){
switch(i){
case 5:
lock.lock();
System.out.print("Locked : ");
lock.unlock();
break;
case 6:
lock.lock();
System.out.print("Locked : ");
lock.unlock();
break;
case 7:
lock.lock();
System.out.print("Locked : ");
lock.unlock();
break;
}
System.out.println(i);
}
}
}
}
Youtube Link : https://youtu.be/ahBC69_iyk4
import java.util.concurrent.locks.ReentrantLock;
public class sychronizedNumber extends Thread {
public static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) throws InterruptedException {
MyThread();
}
public static void MyThread() throws InterruptedException {
Thread t1 = new Thread() {
public void run() {
for (int i = 1; i <= 10; i++) {
synchronized (this) {
switch (i) {
case 5:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
case 6:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
case 7:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
}
}
System.out.println("The current number is:" + i);
}
}
};
Thread t2 = new Thread() {
public void run() {
for (int i = 1; i <= 10; i++) {
synchronized (this) {
switch (i) {
case 5:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
case 6:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
case 7:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
}
}
System.out.println("The current number is:" + i);
}
}
};
Thread t3 = new Thread() {
public void run() {
for (int i = 1; i <= 10; i++) {
synchronized (this) {
switch (i) {
case 5:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
case 6:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
case 7:
lock.lock();
System.out.print("Lock ");
lock.unlock();
break;
}
}
System.out.println("The current number is:" + i);
}
}
};
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
}
package LockThread; import java.util.concurrent.locks.ReentrantLock; class Counter implements Runnable { private String threadName; ReentrantLock lock; Counter(String threadName, ReentrantLock lock){ this.threadName = threadName; this.lock = lock; }
public void run() {
// acquiring the lock
lock.lock();
try {
System.out.println("Thread " + threadName + " has got lock");
Share.count++;
System.out.println("Thread " + threadName +
" Count " + Share.count);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally{
System.out.println("Thread " + threadName
+ " releasing lock");
// releasing the lock
lock.unlock();
}
}
}
package LockThread; import java.util.concurrent.locks.ReentrantLock; public class Lock { public static void main(String[] args) { ReentrantLock rLock = new ReentrantLock(); Thread t1 = new Thread(new Counter("Thread-1", rLock)); Thread t2 = new Thread(new Counter("Thread-2", rLock)); System.out.println("starting threads "); t1.start(); t2.start(); } }
package LockThread;
// Shared class for threads class Share{ static int count = 0; }
package lock;
import java.util.concurrent.locks.ReentrantLock;
/*
@author Ahmed_Bawazir */ public class Lock {
public static ReentrantLock lock = new ReentrantLock(); /**
@param args the command line arguments */ public static void main(String[] args) throws InterruptedException { Thread a = new Thread1(); Thread b = new Thread2(); Thread c = new Thread3();
a.start(); a.join(); System.out.println("==================="); b.start(); b.join(); System.out.println("==================="); c.start(); c.join(); }
static class Thread1 extends Thread{ public void run(){ for(int i=1;i<=10;i++){ lock.lock(); System.out.println(i); lock.unlock(); }
}
}
static class Thread2 extends Thread{ public void run(){ for(int i=1;i<=10;i++){ lock.lock(); System.out.println(i); lock.unlock(); }
}
}
static class Thread3 extends Thread{ public void run(){ for(int i=1;i<=10;i++){ lock.lock(); System.out.println(i); lock.unlock(); }
}
}
}
youtube link https://youtu.be/fjMTaVykOpc
package src;
import java.util.concurrent.locks.ReentrantLock;
class test01 {
public static void main(String[] args) throws InterruptedException{
Thread t1 = new thread01();
Thread t2 = new thread02();
Thread t3 = new thread03();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
}
class thread01 extends Thread{
private final ReentrantLock lock = new ReentrantLock();
public void run(){
lock.lock();
for(int num = 1; num<=10; num++){
if(num==5|| num==6|| num==7){
System.out.println("Lock"+num);
}else{
System.out.println(num);
}
}lock.unlock();
}
}
class thread02 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
public void run(){
lock.lock();
for(int num = 1; num<=10; num++){
if(num==5|| num==6|| num==7){
System.out.println("Lock"+num);
}else{
System.out.println(num);
}
}
lock.unlock();
}
}
class thread03 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
public void run(){
for(int num = 1; num<=10; num++){
if(num==5|| num==6|| num==7){
lock.lock();
System.out.println("Lock"+num);
lock.unlock();
}else{
System.out.println(num);
}
}
}
}
package lock;
import java.util.concurrent.locks.ReentrantLock;
/*
@author Nora98 */ public class Lock {
public static ReentrantLock lock = new ReentrantLock();
public static void main (String [] args) throws Exception { Thread one = new Thread(new Runnable() { @Override public void run(){
for (int a=1; a<=10; a++){
synchronized (this){
switch (a) {
case 5:
case 6:
case 7:
System.out.print("num: ");
break;
}
lock.lock();
System.out.println(a);
lock.unlock();
}
}
} });
Thread two = new Thread(new Runnable() {
@Override
public void run(){
for (int a=1; a<=10; a++){
synchronized (this){
switch (a) {
case 5:
case 6:
case 7:
System.out.print("num: ");
break;
}
lock.lock();
System.out.println(a);
lock.unlock();
} } } });
Thread three = new Thread(new Runnable() { @Override public void run(){
for (int a=1; a<=10; a++){
synchronized (this){
switch (a) {
case 5:
case 6:
case 7:
System.out.print("num: ");
break;
}
lock.lock();
System.out.println(a);
lock.unlock();
} } } });
one.start();
one.join();
two.start();
two.join();
three.start();
three.join();
} }
youtube link: https://www.youtube.com/watch?v=Qy81TtAkmQw
package Thread;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* @author Asus
*/
class Thread1 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
public void run(){
lock.lock();
try{
for (int i = 1; i<=10; i++){
switch(i){
case 5:
System.out.print("Thread1 Lock: ");
break;
case 6:
System.out.print("Thread1 Lock: ");
break;
case 7:
System.out.print("Thread1 Lock: ");
break;
}
System.out.println(i);
}
}catch (Exception e){
lock.unlock();
e.printStackTrace();
}
}
}
package Thread;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* @author Asus
*/
class Thread2 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
public void run(){
lock.lock();
try{
for (int i = 1; i<=10; i++){
switch(i){
case 5:
System.out.print("Thread2 Lock: ");
break;
case 6:
System.out.print("Thread2 Lock: ");
break;
case 7:
System.out.print("Thread2 Lock: ");
break;
}
System.out.println(i);
}
}catch (Exception e){
lock.unlock();
e.printStackTrace();
}
}
}
package Thread;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* @author Asus
*/
class Thread3 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
public void run(){
lock.lock();
try{
for (int i = 1; i<=10; i++){
switch(i){
case 5:
System.out.print("Thread3 Lock: ");
break;
case 6:
System.out.print("Thread3 Lock: ");
break;
case 7:
System.out.print("Thread3 Lock: ");
break;
}
System.out.println(i);
}
}catch (Exception e){
lock.unlock();
e.printStackTrace();
}
}
}
import java.util.concurrent.locks.ReentrantLock;
public class Lock {
static int number1=0;
public static ReentrantLock lock = new ReentrantLock();
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
// TODO code application logic here
Thread t1 = new threadA();
Thread t2 = new threadB();
Thread t3 = new threadC();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
static class threadA extends Thread{
public void run(){
for (int number = 1; number<=10 ; number++){
if(number==5 || number ==6||number ==7) {
lock.lock();
System.out.println("Thread A Lock : "+number);
lock.unlock();
}else{
System.out.println("Thread A : "+number);
}
}
}
}
static class threadB extends Thread{
public void run(){
for (int number = 1; number<=10 ; number++){
if(number==5 || number ==6||number ==7) {
lock.lock();
System.out.println("Thread B Lock : "+number);
lock.unlock();
}else{
System.out.println("Thread B : "+number);
}
}}
}
static class threadC extends Thread{
public void run(){
for (int number = 1; number<=10 ; number++){
if(number==5 || number ==6||number ==7) {
lock.lock();
System.out.println("Thread C Lock : "+number);
lock.unlock();
}else{
System.out.println("Thread C : "+number);
}}
}
}
}
import java.util.concurrent.locks.ReentrantLock;
public class one {
private static ReentrantLock lock = new ReentrantLock();
private static void accessResource() {
for (int i = 1; i <= 10; i++) {
switch (i) {
case 5:
lock.lock();
{
for (int j = 5; j <= 7; j++) {
System.out.println(Thread.currentThread().getName() + "-" + j + " lock");
}
}
i = 8;
lock.unlock();
break;
default:
System.out.println(Thread.currentThread().getName() + "-" + i);
}
}
}
public static void main(String[] args) {
Thread t1 = new Thread(() -> accessResource(), "Thread 1");
t1.start();
Thread t2 = new Thread(() -> accessResource(), "Thread 2");
t2.start();
Thread t3 = new Thread(() -> accessResource(), "Thread 3");
t3.start();
}
}
Instruction
Study the following links:
Go to Youtube, and find the relevant videos. Then rewrite your solution at issue #8 using Lock and ReentrantLock.
Submission