Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the characteristics of java read-write locks

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "what are the characteristics of java read-write locks". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the characteristics of java read-write locks"?

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 ();}} so far, I believe you have a deeper understanding of "what are the characteristics of java read-write locks". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report