- Given shared resources in java.
- Synchronize shares resources using Lock interface.
- We will use ReentrantLock to protect share resources.
- A lock is a tool for controlling access to a shared resource by multiple threads.
- Lock implementations provide more extensive locking operations than can be obtained using synchronized methods and statements.
- We create five threads, which will be used to transfer file from one system to another.
- We will use Lock interface to synchronize file transfer threads.
1. TransferFile Class:
- TransferFile class is responsible for transferring file from one machine to another machine.
- We will generate a random number and introduce sleep to simulate the time taken to transfer file.
package org.learn.sync.lk; import java.io.File; import java.util.Random; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import static java.lang.Thread.sleep; public class TransferFile { private Lock lock = new ReentrantLock(); private Random random = new Random(); public void transfer(File file /*file to transfer*/ ) { int time = random.nextInt( 200 ); lock.lock(); try { System.out.printf( "Sending file to another machine,time required: %d\n" , time); //transferring file to another server machine sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } } |
2. FileTransferJob Class:
- FileTransferJob class is Thread class.
- FileTransferJob class invoke TranferFile class, which actually transfer file from one system to another system.
- In run method, we pass dummy file to TransferFile class.
package org.learn.sync.lk; import java.io.File; public class FileTransferJob extends Thread { private TransferFile transferFile; public FileTransferJob(TransferFile transferFile) { this .transferFile = transferFile; } @Override public void run() { File dummyFile = new File( "" ); System.out.printf( "%s, Start sending dummy file to another machine\n" ,getName()); transferFile.transfer(dummyFile); System.out.printf( "%s,Successfully sent file to another machine\n" ,getName()); } } |
3. LockExample Class:
- LockExample class containing main method to test lock (or ReentrantLock) in java.
- We will create five FileTransferJob Threads to transfer from file one system to another system.
package org.learn.sync.lk; public class LockExample { public static void main(String[] args) throws InterruptedException { for ( int job = 0 ; job < 5 ; job++) { FileTransferJob transferJob = new FileTransferJob( new TransferFile()); transferJob.start(); } } } |
4. Output: Synchronize resources using ReentrantLock in java (example)
Thread- 0 , Start sending dummy file to another machine Thread- 4 , Start sending dummy file to another machine Thread- 3 , Start sending dummy file to another machine Thread- 2 , Start sending dummy file to another machine Thread- 1 , Start sending dummy file to another machine Sending file to another machine,time required: 47 Sending file to another machine,time required: 81 Sending file to another machine,time required: 126 Sending file to another machine,time required: 8 Sending file to another machine,time required: 44 Thread- 0 ,Successfully sent file to another machine Thread- 1 ,Successfully sent file to another machine Thread- 2 ,Successfully sent file to another machine Thread- 3 ,Successfully sent file to another machine Thread- 4 ,Successfully sent file to another machine |