A principal pesquisa sobre os recursos do ReadWritelock neste artigo, como segue.
Readlock e Readlock não são mutuamente exclusivos
Readlock e Writelock são mutuamente exclusivos
Writelock e Readlock são mutuamente exclusivos
Writelock e WriteLock são mutuamente exclusivos
Thread 1, get readLock first, thread 2 tries to get readLock, you can get thread 1, get readLock first, thread 2 tries to get writeLock, blocking and waiting, and you can get thread 1 until thread 1 releases the lock, and you can get thread 1, get writeLock first, thread 1, get writeLock first, thread 2 tries to get writeLock, blocking and waiting, and you can get thread 1 until thread 1 releases the lock.
pacote com.alio.lock; importar java.text.simpledEformat; importar java.util.date; importar java.util.concurrent.locks.lock; import java.util.concurrent.locks.readwritelock; import java.util.oncurntent.locks.lock.locks.readwrock; import java.util.concurnt.locks.rentrent.locks.readwrock; significativa.util.Curntent.Locks.rentrent.locks.readwrock; importância.util.Curntent.Locks.rentrent.locks.Read.Writelock; significativa.util.Curntent.Locks.rentrent.locks.readwrock; significativa.util.Curntent.Locks.rentrent.locks.Rereads; ReadWritelockDemo {estático SimpleDateFormat sdf = new SimpledateFormat ("AAA MM-DD HH: MM: SS.SS"); public static void main (String [] args) {Data Data = new Data (); trabalhador T1 = novo trabalhador (Dados, False); // Trabalhador t2 t2 = Dados = Dados (); Dados; true); // leia t1.start (); t2.start (); t3.start ();} O trabalhador da classe estática estende o thread {dados de dados; Boolean Read; Public Worker (dados de dados, leitura booleana) {this.data = data; this.read = leia;} public void run () {if (read) data. (); caso contrário, data.Write ();}} Dados da classe estática {readWritelock Lock = new ReentrantreadWriteLock (); Lock read = Lock.readlock (); Lock Write = Lock.Writelock (); public void Write () {Try {Thread.sleep (2000); {} write.lock (); system.out.println (thread.currentThread () + "write: BEGN" + sdf.format (new Date ())); tente {thread.sleep (5000); //} catch (exceção e) {} finalmente {system.out.println (shread.curntth () ({} finalmente {system.out.println); Date ())); write.unlock ();}} public int read () {read.lock (); system.out.println (thread.currentThread () + "leia: begin" + sdf.format (new date ()); try {thread.sleep (5000); + "Leia: end" + sdf.format (new date ())); read.unlock ();} retornar 1;}}}Tópico [Thread-2,5, Main] Leia: Begin 2018-01-22 13: 54: 16.794
Tópico [Thread-1,5, Main] Leia: Begin 2018-01-22 13: 54: 16.794
Tópico [Thread-2,5, Main] Leia: Fim 2018-01-22 13: 54: 21.795
Tópico [Thread-1,5, Main] Leia: Fim 2018-01-22 13: 54: 21.795
Thread [Thread-0,5, Main] Write: Begin 2018-01-22 13: 54: 21.795
Tópico [Thread-0,5, Main] Write: Fim 2018-01-22 13: 54: 26.795
Três tópicos foram iniciados ao mesmo tempo, entre os quais o thread nº 1 thread Thread[Thread-0,5,main], e ele dorme por 2 segundos ao executar a gravação. Em seguida, Thread No. 2, Thread No. 3 Thread[Thread-1,5,main],Thread[Thread-2,5,main] priorizará a execução do código
read.lock ();
Desde read.lock(); não é mutuamente exclusivo (pode ser reentrante), eles obtiveram a trava ao mesmo tempo, o que pode ser visto através do log
Tópico [Thread-2,5, Main] Leia: Begin 2018-01-22 13: 54: 16.794Thread [Thread-1,5, Main] Leia: Begin 2018-01-22 13: 54: 16.794
E o tempo de execução do tempo de execução é o mesmo (o código de teste está dormindo por 5 segundos), portanto a execução também terminará ao mesmo tempo.
Tópico [Thread-2,5, Main] Leia: Fim 2018-01-22 13: 54: 21.795Thread [Thread-1,5, Main] Leia: Fim 2018-01-22 13: 54: 21.795
Somente quando todas as leituras são lançadas, o WritElock obtém o bloqueio. No momento, o bloqueio será liberado após a execução do Thread [Thread-1,5, Main] e Thread [Thread-2,5, Main] será lançado.
Então Thread[Thread-0,5,main] obteve WriteLock e executou seu próprio código de negócios no momento
Tópico [Thread-0,5, Main] Write: Begin 2018-01-22 13: 54: 21.795Thread [Thread-0,5, Main] Write: Fim 2018-01-22 13: 54: 26.795
O exposto acima é tudo sobre o teste de exemplo do recurso Java Language ReadWritelock, espero que seja útil para todos. Amigos interessados podem continuar se referindo a outros tópicos relacionados neste site. Se houver alguma falha, deixe uma mensagem para apontá -la. Obrigado amigos pelo seu apoio para este site!