Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes
CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

Java > Core Java Code Examples

Creating a Shared File Lock on a File

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
Creating a Shared File Lock on a File public static void main(String[] args) { try { // Obtain a file channel File file = new File("filename"); FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); // Create a shared lock on the file. // This method blocks until it can retrieve the lock. FileLock lock = channel.lock(0, Long.MAX_VALUE, true); // Try acquiring a shared lock without blocking. This method returns // null or throws an exception if the file is already exclusively locked. try { lock = channel.tryLock(0, Long.MAX_VALUE, true); } catch (OverlappingFileLockException e) { // File is already locked in this thread or virtual machine } // Determine the type of the lock boolean isShared = lock.isShared(); // Release the lock lock.release(); // Close the file channel.close(); } catch (Exception e) { } }
Adding Items and Removing Itesm from JList
Currency Formatter
Creating a File Lock on a File
Reporter bean
Callable statement example
Lawyer bean
Serialise object to file
Serializing an Object
Validating schema
Parsing and Formatting a Number into Binary, Octal, and Hexadecimal
Drawing Shapes in Java 2D
Scribble into a canvas