This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.io.RandomAccessFile; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.FileChannel; | |
import java.nio.channels.FileLock; | |
import java.nio.channels.OverlappingFileLockException; | |
public class ConcurrentPartialFileLocking { | |
private static final String FILENAME = "concurrent_partial_locked_file.txt"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void createFileWithPhrase(String filename, String phrase) { | |
File file = new File(filename); | |
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw")) { | |
randomAccessFile.write(phrase.getBytes()); // Write the string as bytes. | |
System.out.println("File " + filename + " created with the phrase: \"" + phrase + "\""); | |
} catch (IOException e) { | |
System.err.println("Error creating file: " + e.getMessage()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void run() { | |
File file = new File(filename); | |
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); | |
FileChannel channel = randomAccessFile.getChannel()) { | |
System.out.println(threadName + " attempting to acquire a lock on region [" + | |
startPosition + ", " + (startPosition + regionSize) + "]"); | |
FileLock lock = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.io.RandomAccessFile; | |
import java.nio.channels.FileChannel; | |
import java.nio.channels.FileLock; | |
import java.nio.channels.OverlappingFileLockException; | |
public class FileLockingExample { | |
public static void main(String[] args) { | |
File file = new File("locked_file.txt"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class C extends Thread | |
{ | |
private Semaphore[] resource; | |
public C( Semaphore[] m ) { | |
resource = m; | |
} | |
public void run( ) { | |
System.out.println( "C started" ); | |
synchronized ( resource[2] ) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class B extends Thread { | |
private Semaphore[] resource; | |
public B(Semaphore[] m) { | |
resource = m; | |
} | |
public void run() { | |
System.out.println("B started"); | |
synchronized (resource[3]) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class A extends Thread { | |
private Semaphore[] resource; | |
public A(Semaphore[] m) { | |
resource = m; | |
} | |
public void run( ) { | |
System.out.println( "A started" ); | |
synchronized ( resource[1] ) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.Semaphore; | |
public class Deadlock { | |
public Deadlock( ) { | |
Semaphore mutex[] = new Semaphore[4]; | |
for ( int i = 0; i < 4; i++ ) | |
mutex[i] = new Semaphore(1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class App { | |
public static void main(String[] args) { | |
ResourceAllocator allocator = new ResourceAllocator(); | |
// Example usage with multiple "Processes" | |
Runnable P0 = ( ) -> { | |
... // same code, different critical section time = 1000 | |
}; | |
Runnable P1 = () -> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.locks.Condition; | |
import java.util.concurrent.locks.Lock; | |
import java.util.concurrent.locks.ReentrantLock; | |
public class ResourceAllocator { | |
private boolean busy; | |
private final Lock lock = new ReentrantLock(); | |
private final Condition resourceAvailable = lock.newCondition(); |
NewerOlder