31
31
import java .nio .charset .Charset ;
32
32
import java .nio .charset .IllegalCharsetNameException ;
33
33
import java .nio .charset .UnsupportedCharsetException ;
34
- import java .util .concurrent .locks .Lock ;
35
- import java .util .concurrent .locks .ReentrantLock ;
36
34
import jdk .internal .access .JavaIOPrintWriterAccess ;
37
35
import jdk .internal .access .SharedSecrets ;
36
+ import jdk .internal .misc .InternalLock ;
38
37
39
38
/**
40
39
* Prints formatted representations of objects to a text-output stream. This
@@ -117,9 +116,9 @@ public PrintWriter(Writer out,
117
116
this .out = out ;
118
117
this .autoFlush = autoFlush ;
119
118
120
- // use ReentrantLock when PrintWriter is not sub-classed
119
+ // use ExplicitLock when PrintWriter is not sub-classed
121
120
if (getClass () == PrintWriter .class ) {
122
- this .lock = new ReentrantLock ();
121
+ this .lock = new InternalLock ();
123
122
}
124
123
}
125
124
@@ -400,13 +399,12 @@ private void ensureOpen() throws IOException {
400
399
*/
401
400
public void flush () {
402
401
Object lock = this .lock ;
403
- if (lock instanceof Lock ) {
404
- Lock theLock = (Lock ) lock ;
405
- theLock .lock ();
402
+ if (lock instanceof InternalLock locker ) {
403
+ locker .lock ();
406
404
try {
407
405
lockedFlush ();
408
406
} finally {
409
- theLock .unlock ();
407
+ locker .unlock ();
410
408
}
411
409
} else {
412
410
synchronized (lock ) {
@@ -432,13 +430,12 @@ private void lockedFlush() {
432
430
*/
433
431
public void close () {
434
432
Object lock = this .lock ;
435
- if (lock instanceof Lock ) {
436
- Lock theLock = (Lock ) lock ;
437
- theLock .lock ();
433
+ if (lock instanceof InternalLock locker ) {
434
+ locker .lock ();
438
435
try {
439
436
lockedClose ();
440
437
} finally {
441
- theLock .unlock ();
438
+ locker .unlock ();
442
439
}
443
440
} else {
444
441
synchronized (lock ) {
@@ -513,13 +510,12 @@ protected void clearError() {
513
510
*/
514
511
public void write (int c ) {
515
512
Object lock = this .lock ;
516
- if (lock instanceof Lock ) {
517
- Lock theLock = (Lock ) lock ;
518
- theLock .lock ();
513
+ if (lock instanceof InternalLock locker ) {
514
+ locker .lock ();
519
515
try {
520
516
lockedWrite (c );
521
517
} finally {
522
- theLock .unlock ();
518
+ locker .unlock ();
523
519
}
524
520
} else {
525
521
synchronized (lock ) {
@@ -552,13 +548,12 @@ private void lockedWrite(int c) {
552
548
*/
553
549
public void write (char buf [], int off , int len ) {
554
550
Object lock = this .lock ;
555
- if (lock instanceof Lock ) {
556
- Lock theLock = (Lock ) lock ;
557
- theLock .lock ();
551
+ if (lock instanceof InternalLock locker ) {
552
+ locker .lock ();
558
553
try {
559
554
lockedWrite (buf , off , len );
560
555
} finally {
561
- theLock .unlock ();
556
+ locker .unlock ();
562
557
}
563
558
} else {
564
559
synchronized (lock ) {
@@ -600,13 +595,12 @@ public void write(char buf[]) {
600
595
*/
601
596
public void write (String s , int off , int len ) {
602
597
Object lock = this .lock ;
603
- if (lock instanceof Lock ) {
604
- Lock theLock = (Lock ) lock ;
605
- theLock .lock ();
598
+ if (lock instanceof InternalLock locker ) {
599
+ locker .lock ();
606
600
try {
607
601
lockedWrite (s , off , len );
608
602
} finally {
609
- theLock .unlock ();
603
+ locker .unlock ();
610
604
}
611
605
} else {
612
606
synchronized (lock ) {
@@ -637,13 +631,12 @@ public void write(String s) {
637
631
638
632
private void newLine () {
639
633
Object lock = this .lock ;
640
- if (lock instanceof Lock ) {
641
- Lock theLock = (Lock ) lock ;
642
- theLock .lock ();
634
+ if (lock instanceof InternalLock locker ) {
635
+ locker .lock ();
643
636
try {
644
637
lockedNewLine ();
645
638
} finally {
646
- theLock .unlock ();
639
+ locker .unlock ();
647
640
}
648
641
} else {
649
642
synchronized (lock ) {
@@ -809,14 +802,13 @@ public void println() {
809
802
*/
810
803
public void println (boolean x ) {
811
804
Object lock = this .lock ;
812
- if (lock instanceof Lock ) {
813
- Lock theLock = (Lock ) lock ;
814
- theLock .lock ();
805
+ if (lock instanceof InternalLock locker ) {
806
+ locker .lock ();
815
807
try {
816
808
print (x );
817
809
println ();
818
810
} finally {
819
- theLock .unlock ();
811
+ locker .unlock ();
820
812
}
821
813
} else {
822
814
synchronized (lock ) {
@@ -835,14 +827,13 @@ public void println(boolean x) {
835
827
*/
836
828
public void println (char x ) {
837
829
Object lock = this .lock ;
838
- if (lock instanceof Lock ) {
839
- Lock theLock = (Lock ) lock ;
840
- theLock .lock ();
830
+ if (lock instanceof InternalLock locker ) {
831
+ locker .lock ();
841
832
try {
842
833
print (x );
843
834
println ();
844
835
} finally {
845
- theLock .unlock ();
836
+ locker .unlock ();
846
837
}
847
838
} else {
848
839
synchronized (lock ) {
@@ -861,14 +852,13 @@ public void println(char x) {
861
852
*/
862
853
public void println (int x ) {
863
854
Object lock = this .lock ;
864
- if (lock instanceof Lock ) {
865
- Lock theLock = (Lock ) lock ;
866
- theLock .lock ();
855
+ if (lock instanceof InternalLock locker ) {
856
+ locker .lock ();
867
857
try {
868
858
print (x );
869
859
println ();
870
860
} finally {
871
- theLock .unlock ();
861
+ locker .unlock ();
872
862
}
873
863
} else {
874
864
synchronized (lock ) {
@@ -887,14 +877,13 @@ public void println(int x) {
887
877
*/
888
878
public void println (long x ) {
889
879
Object lock = this .lock ;
890
- if (lock instanceof Lock ) {
891
- Lock theLock = (Lock ) lock ;
892
- theLock .lock ();
880
+ if (lock instanceof InternalLock locker ) {
881
+ locker .lock ();
893
882
try {
894
883
print (x );
895
884
println ();
896
885
} finally {
897
- theLock .unlock ();
886
+ locker .unlock ();
898
887
}
899
888
} else {
900
889
synchronized (lock ) {
@@ -913,14 +902,13 @@ public void println(long x) {
913
902
*/
914
903
public void println (float x ) {
915
904
Object lock = this .lock ;
916
- if (lock instanceof Lock ) {
917
- Lock theLock = (Lock ) lock ;
918
- theLock .lock ();
905
+ if (lock instanceof InternalLock locker ) {
906
+ locker .lock ();
919
907
try {
920
908
print (x );
921
909
println ();
922
910
} finally {
923
- theLock .unlock ();
911
+ locker .unlock ();
924
912
}
925
913
} else {
926
914
synchronized (lock ) {
@@ -939,14 +927,13 @@ public void println(float x) {
939
927
*/
940
928
public void println (double x ) {
941
929
Object lock = this .lock ;
942
- if (lock instanceof Lock ) {
943
- Lock theLock = (Lock ) lock ;
944
- theLock .lock ();
930
+ if (lock instanceof InternalLock locker ) {
931
+ locker .lock ();
945
932
try {
946
933
print (x );
947
934
println ();
948
935
} finally {
949
- theLock .unlock ();
936
+ locker .unlock ();
950
937
}
951
938
} else {
952
939
synchronized (lock ) {
@@ -965,14 +952,13 @@ public void println(double x) {
965
952
*/
966
953
public void println (char x []) {
967
954
Object lock = this .lock ;
968
- if (lock instanceof Lock ) {
969
- Lock theLock = (Lock ) lock ;
970
- theLock .lock ();
955
+ if (lock instanceof InternalLock locker ) {
956
+ locker .lock ();
971
957
try {
972
958
print (x );
973
959
println ();
974
960
} finally {
975
- theLock .unlock ();
961
+ locker .unlock ();
976
962
}
977
963
} else {
978
964
synchronized (lock ) {
@@ -991,14 +977,13 @@ public void println(char x[]) {
991
977
*/
992
978
public void println (String x ) {
993
979
Object lock = this .lock ;
994
- if (lock instanceof Lock ) {
995
- Lock theLock = (Lock ) lock ;
996
- theLock .lock ();
980
+ if (lock instanceof InternalLock locker ) {
981
+ locker .lock ();
997
982
try {
998
983
print (x );
999
984
println ();
1000
985
} finally {
1001
- theLock .unlock ();
986
+ locker .unlock ();
1002
987
}
1003
988
} else {
1004
989
synchronized (lock ) {
@@ -1020,14 +1005,13 @@ public void println(String x) {
1020
1005
public void println (Object x ) {
1021
1006
String s = String .valueOf (x );
1022
1007
Object lock = this .lock ;
1023
- if (lock instanceof Lock ) {
1024
- Lock theLock = (Lock ) lock ;
1025
- theLock .lock ();
1008
+ if (lock instanceof InternalLock locker ) {
1009
+ locker .lock ();
1026
1010
try {
1027
1011
print (s );
1028
1012
println ();
1029
1013
} finally {
1030
- theLock .unlock ();
1014
+ locker .unlock ();
1031
1015
}
1032
1016
} else {
1033
1017
synchronized (lock ) {
@@ -1180,13 +1164,12 @@ public PrintWriter printf(Locale l, String format, Object ... args) {
1180
1164
*/
1181
1165
public PrintWriter format (String format , Object ... args ) {
1182
1166
Object lock = this .lock ;
1183
- if (lock instanceof Lock ) {
1184
- Lock theLock = (Lock ) lock ;
1185
- theLock .lock ();
1167
+ if (lock instanceof InternalLock locker ) {
1168
+ locker .lock ();
1186
1169
try {
1187
1170
lockedFormat (format , args );
1188
1171
} finally {
1189
- theLock .unlock ();
1172
+ locker .unlock ();
1190
1173
}
1191
1174
} else {
1192
1175
synchronized (lock ) {
@@ -1255,13 +1238,12 @@ private void lockedFormat(String format, Object ... args) {
1255
1238
*/
1256
1239
public PrintWriter format (Locale l , String format , Object ... args ) {
1257
1240
Object lock = this .lock ;
1258
- if (lock instanceof Lock ) {
1259
- Lock theLock = (Lock ) lock ;
1260
- theLock .lock ();
1241
+ if (lock instanceof InternalLock locker ) {
1242
+ locker .lock ();
1261
1243
try {
1262
1244
lockedFormat (l , format , args );
1263
1245
} finally {
1264
- theLock .unlock ();
1246
+ locker .unlock ();
1265
1247
}
1266
1248
} else {
1267
1249
synchronized (lock ) {
0 commit comments