Java multithreading ~~~ Multi-thread Implementation of ReadWriteLock read/write splitting, javareadwritelock
In multi-threaded development, there is often a situation where we want read/write splitting. For reading this action, there can be multiple threads at the same time
But for writing this action, only one thread can operate at the same time, and at the same time, when a write thread operates this resource
When the source is used, other read threads cannot operate on this resource. In this way, the multi-thread feature is brought into full play and the multi-thread capability can be well utilized.
.
In Java, the ReadWriteLock interface implements this requirement for us. Through its implementation class ReentrantReadWriteLock, we can
To achieve the effect just now, we will use an example to illustrate the usage of this class.
Package com. bird. concursey. charpet3; import java. util. concurrent. locks. readWriteLock; import java. util. concurrent. locks. reentrantReadWriteLock; public class PricesInfo {private double price1; private double price2; private ReadWriteLock lock; public PricesInfo () {price1 = 1.0; price2 = 2.0; lock = new ReentrantReadWriteLock ();} public double getPrice1 () {// read resource lock. readLock (). lock (); double value = price1; lock. readLock (). unlock (); return value;} public double getPrice2 () {lock. readLock (). lock (); double value = price2; lock. readLock (). unlock (); return value;} public void setPrices (double price1, double price2) {lock. writeLock (). lock (); this. price1 = price1; this. price2 = price2; lock. writeLock (). unlock ();}}
The following are two different read and write classes.
package com.bird.concursey.charpet3;public class Reader implements Runnable {private PricesInfo pricesInfo;public Reader(PricesInfo pricesInfo) {this.pricesInfo = pricesInfo;}@Overridepublic void run() {for (int i = 0; i < 10; i++) {System.out.printf("%s: Price 1: %f\n", Thread.currentThread().getName(), pricesInfo.getPrice1());System.out.printf("%s: Price 2: %f\n", Thread.currentThread().getName(), pricesInfo.getPrice2());}}}
package com.bird.concursey.charpet3;public class Writer implements Runnable {private PricesInfo pricesInfo;public Writer(PricesInfo pricesInfo) {this.pricesInfo = pricesInfo;}@Overridepublic void run() {for (int i = 0; i < 3; i++) {System.out.printf("Writer: Attempt to modify the prices.\n");pricesInfo.setPrices(Math.random() * 10, Math.random( ) * 8);System.out.printf("Writer: Prices have been modified.\n");try {Thread.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}}}public static void main(String[] args) {PricesInfo pricesInfo = new PricesInfo();Reader readers[] = new Reader[5];Thread threadsReader[] = new Thread[5];for (int i = 0; i < 5; i++){readers[i] = new Reader(pricesInfo);threadsReader[i] = new Thread(readers[i]);}Writer writer = new Writer(pricesInfo);Thread threadWriter = new Thread(writer);for (int i = 0; i < 5; i++){threadsReader[i].start();}threadWriter.start();}}
Is this method feasible if java multiple threads read a file at the same time?
Import java. io .*;
Class DownThread extends Thread {
// Define the length of the byte array (the bamboo for taking water)
Private final int BUFF_LEN = 32;
// Define the start point for reading
Private long start;
// Define the read end point
Private long end;
// Read the input stream corresponding to the file
Private InputStream is;
// Output the read bytes to raf.
Private RandomAccessFile raf;
// Constructor: input stream, output stream, read start point, and end point
Public DownThread (long start, long end, InputStream is, RandomAccessFile raf ){
// Output the byte location that the thread is responsible for reading
System. out. println (start + "---->" + end );
This. start = start;
This. end = end;
This. is = is;
This. raf = raf;
}
Public void run (){
Try {
Is. skip (start );
Raf. seek (start );
// Define the cache array for reading the content of the input stream (bamboo)
Byte [] buff = new byte [BUFF_LEN];
// This thread is responsible for reading the file size
Long contentLen = end-start;
// Define the maximum number of reads required for this thread.
Long times = contentLen/BUFF_LEN + 4;
// The actual number of bytes read
Int hasRead = 0;
For (int I = 0; I <times; I ++ ){
HasRead = is. read (buff );
// Exit the loop if the number of bytes read is smaller than 0!
If (hasRead <0 ){
Break;
}
Raf. write (buff, 0, hasRead );
}
} Catch (Exception ex ){
Ex. printStackTrace ();
}
// Use finally blocks to close the input and output streams of the current thread
Finally {
... The remaining full text>
Comparison of Java multithreading implementation methods
Runnable is an interface with no start () method defined. Only the run () method is used. (extends) is the method that inherits the Thread and all the methods that can use it.