In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge of this article "how to use java read-write Lock", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use java read-write Lock" article.
1. Fair and selective, support unfair and fair lock acquisition, throughput unfair is better than fair.
2. Reentry, read lock and write lock all support thread reentry.
3, the lock is degraded, following the order of acquiring the write lock, acquiring the read lock, and releasing the write lock, the write lock can be degraded to a read lock.
Example
Public class ReadWriteLockTest {public static void main (String [] args) {final Queue q = new Queue (); for (int I = 0; I < 3; iTunes +) {new Thread () {@ Override public void run () {while (true) {q.get () } .start (); new Thread () {@ Override public void run () {while (true) {q.put (new Random () .nextInt (10000)) }. Start ();}} class Queue {/ / shared data, only one thread can write the data, but multiple threads can read the data at the same time. ReadWriteLock rwl = new ReentrantReadWriteLock (); private Object data = null;// share data, only one thread can write data, but multiple threads can read the data public void get () {/ / lock, other threads can only read and not write rwl.readLock (). Lock (); try {System.out.println (Thread.currentThread (). GetName () + "be ready to read data!") Thread.sleep ((long) (Math.random () * 1000)); System.out.println (Thread.currentThread (). GetName () + "have read data:" + data);} catch (InterruptedException e) {e.printStackTrace ();} finally {rwl.readLock () .unlock () }} public void put (Object data) {/ / write lock, do not allow other threads to read or write rwl.writeLock (). Lock (); try {System.out.println (Thread.currentThread (). GetName () + "be ready to write data!"); Thread.sleep ((long) (Math.random () * 1000)); this.data = data System.out.println (Thread.currentThread (). GetName () + "have write data:" + data);} catch (InterruptedException e) {e.printStackTrace ();} finally {rwl.writeLock () .unlock () } the above is about the content of this article on "how to use java read-write locks". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.