Search
πŸ”’

Java Synchronized with String intern()

Date
2024/02/24
Tag
Java
Thread
숨기기

Synchronized

JavaλŠ” λ©€ν‹° μŠ€λ ˆλ“œ ν™˜κ²½μ—μ„œ λ™μ‹œμ„± μ œμ–΄λ₯Ό ν•˜κΈ° μœ„ν•΄ μ—¬λŸ¬ 방법을 μ§€μ›ν•œλ‹€.
β€’
Lock
β€’
Synchronized
β€’
Concurrent Collections
β€’
λ“±λ“±
SynchronizedλŠ” λ™μ‹œμ„± μ œμ–΄λ₯Ό μœ„ν•œ κ°€μž₯ 기초적인 λ°©λ²•μœΌλ‘œ, νŠΉμ • 객체λ₯Ό 잠금(lock)으둜써 μž„κ³„ μ˜μ—­μ„ μ„€μ •ν•œλ‹€.
synchronized(obj) { // μž„κ³„ μ˜μ—­(critical section) }
Java
볡사
λ©”μ„œλ“œ λŒ€μƒμœΌλ‘œ Synchronizedλ₯Ό μ‚¬μš©ν•  수 μžˆμ§€λ§Œ 이 λ˜ν•œ 클래슀 객체λ₯Ό 잠금(lock) ν•˜μ—¬ μž„κ³„ μ˜μ—­μ„ μ„€μ •ν•˜λŠ” 것이닀.
class foo { synchronized void someAction() { // code } } class foo { void someAction() { synchronized(this) { // code } } }
Java
볡사

주의 사항

ν•˜μ§€λ§Œ Synchronized에 잠금 객체λ₯Ό λ¬Έμžμ—΄(String)을 μ‚¬μš©ν•˜λŠ” 경우 μ£Όμ˜κ°€ ν•„μš”ν•˜λ‹€.
λ§Œμ•½ λ‹€μŒκ³Ό 같이 λ¬Έμžμ—΄μ„ β€œ+” μ—°μ‚°μžλ‘œ ν•©μΉ˜κ³  잠금 객체둜 μ‚¬μš©ν•  경우 λ¬Έμ œκ°€ λ°œμƒν•œλ‹€.
public class SynchronizedString { private int value = 0; public void incrementValue(long id) { synchronized ("lock" + id) { value++; System.out.println("value: " + value); } } public int getValue() { return value; } } @Test @DisplayName("λ¬Έμžμ—΄ 동기화 ν…ŒμŠ€νŠΈ") void test_synchronized_string() throws InterruptedException { // given SynchronizedString synchronizedString = new SynchronizedString(); // when int count = 10000; ExecutorService executorService = Executors.newFixedThreadPool(20); CountDownLatch countDownLatch = new CountDownLatch(count); for (int i = 0; i < count; i++) { executorService.execute(() -> { synchronizedString.incrementValue(1L); countDownLatch.countDown(); }); } countDownLatch.await(); // then // ν…ŒμŠ€νŠΈκ°€ μ‹€νŒ¨ν•œλ‹€. assertEquals(count, synchronizedString.getValue()); }
Java
볡사
μ΄μœ λŠ” (β€œlock” + id) λΆ€λΆ„ λ•Œλ¬Έμ΄λ‹€. Javaμ—μ„œ λ¬Έμžμ—΄μ€ λΆˆλ³€μ„±(immutable)을 κ°€μ§€λŠ”λ°, 이 νŠΉμ„±μœΌλ‘œ λ¬Έμžμ—΄μ€ ν•œλ²ˆ μƒμ„±λ˜λ©΄ μˆ˜μ •μ΄ λΆˆκ°€λŠ₯ν•˜κ³  β€œ+” μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•˜κ²Œ λœλ‹€λ©΄ μƒˆλ‘œμš΄ λ¬Έμžμ—΄ 객체λ₯Ό μƒμ„±ν•˜κ²Œ λœλ‹€. κ·Έλž˜μ„œ νž™ λ©”λͺ¨λ¦¬ 상 μ£Όμ†Œκ°€ λ‹€λ₯΄κΈ° λ•Œλ¬Έμ— 동일 객체둜 μΈμ‹ν•˜μ§€ μ•Šμ•„ μ •μƒμ μœΌλ‘œ 동기화가 λ˜μ§€ μ•ŠλŠ”λ‹€.
μŠ€λ ˆλ“œλ§ˆλ‹€ μƒˆλ‘œμš΄ λ¬Έμžμ—΄μ— lock을 건닀.

ν•΄κ²° 방법

ν•΄κ²° 방법은 μƒˆλ‘œ μƒμ„±ν•œ λ¬Έμžμ—΄μ— λŒ€ν•΄ lock을 κ±°λŠ” 게 μ•„λ‹Œ, String Pool에 μžˆλŠ” λ¬Έμžμ—΄μ— λŒ€ν•΄ lock을 κ±°λŠ” 것이닀. Spring Pool에 μ ‘κ·Όν•˜κΈ° μœ„ν•΄μ„œλŠ” intern() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€. intern() λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄ String Pool에 μ‘΄μž¬ν•˜λŠ” λ¬Έμžμ—΄μ΄λΌλ©΄ ν’€μ˜ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•˜κ³ , μ—†λ‹€λ©΄ 풀에 등둝 ν›„ λ°˜ν™˜ν•œλ‹€.
public void incrementValueWithIntern(long id) { synchronized (("lock" + id).intern()) { value++; System.out.println("value: " + value); } }
Java
볡사
intern() λ©”μ„œλ“œλ‘œ λ™μΌν•œ λ¬Έμžμ—΄μ— lock을 건닀.

μ†ŒμŠ€ μ½”λ“œ

뢀둝

μ‹€μ œλ‘œ 개발 μ‹œ λ¬Έμžμ—΄μ„ synchronized 객체둜 μ‚¬μš©ν•  일은 λ“œλ¬Όκ±°λ‚˜ μ—†κ² μ§€λ§Œ, λ™μ‹œμ„± κ΅¬ν˜„ 과제λ₯Ό μ§„ν–‰ν•˜λ©΄μ„œ μ•Œκ²Œ 된 사싀이닀. μ›λž˜λ„ String Poolκ³Ό intern()에 λŒ€ν•΄μ„œ μ•Œκ³  μžˆμ—ˆμ§€λ§Œ 처음 κ²ͺ은 μΌ€μ΄μŠ€λΌ μ •λ¦¬ν•˜κ²Œ 됐닀.
intern() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ κ²°κ΅­ String Pool에 μžˆλŠ” λ¬Έμžμ—΄μ„ 비ꡐ할 텐데 그럼 속도가 더 λŠλ¦¬μ§€ μ•Šμ„κΉŒ ν•˜μ—¬ 검색해본 λ‚΄μš©μ΄λ‹€.
Java String intern(): Performance impact
java.lang.String#intern() is an interesting function in Java. When used at the right place, it has potential to reduce overall memory consumption of your application by eliminating duplicate strings in your application. In this video, let’s discuss the performance impact of using java.lang.String#intern() function in your application. If you liked this video, you'll love our Java Performance Masterclass. Learn more: https://ycrash.io/java-performance-training - - - - - - - - - - - - - - - Topics covered: 00:12 - Interesting Q&A of Java String Intern() function 00:47 - intern() function Demo 04:25 - Memory impact 07:10 - Duplicate Strings Impact 09:23 - Response time - - - - - - - - - - - - - - - Links shared in the video: java.lang.String#intern(): https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#intern() Java String intern() - Interesting Q & A: https://blog.ycrash.io/2022/08/03/java-string-intern-interesting-q-a/ To download the 'random-data.txt' file: https://tier1app.com/dist/sample/random-data.txt InternDemo Heap analysis report: https://heaphero.io/my-heap-report.jsp?p=YXJjaGl2ZWQvMjAyMi8wNy8xOS8tLWhlYXBkdW1wLTE2NTgyNzQwMDY0MjEuaHByb2YtMjMtNDItNDMuanNvbg== NoInternDemo Heap analysis report: https://heaphero.io/my-heap-report.jsp?p=YXJjaGl2ZWQvMjAyMi8wNy8xOS8tLWhlYXBkdW1wLTE2NTgyNzQwNDg5MjUuemlwLTIzLTQ3LTIyLmpzb24= string.equals() method: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#equals(java.lang.Object) - - - - - - - - - - - - - - - Read more in this blog: https://blog.ycrash.io/2022/08/11/java-string-intern-performance-impact/ - - - - - - - - - - - - - - - Our Tools: yCrash: https://yCrash.io/ GCeasy: https://gceasy.io/ HeapHero: https://heaphero.io/ fastThread Tool: https://fastthread.io/ - - - - - - - - - - - - - - - SRE Community: Need help in troubleshooting your performance problems? Post here: https://answers.ycrash.io/ - - - - - - - - - - - - - - - Follow us on: LinkedIn: https://www.linkedin.com/company/ycrash Instagram: https://www.instagram.com/ycrash_rca/ Facebook: https://www.facebook.com/tier1app Twitter: https://twitter.com/tier1app - - - - - - - - - - - - - - - Related videos: Java String intern() - Interesting Q & A: https://youtu.be/HiL2634pZaA - - - - - - - - - - - - - - - #Javastrings | #manuallyinternstring | #stringinternmethodinjava | #internmethodofstringinjava | #string | #stringsinjava | #strings | #whatisinternmethodinstringclassinjava | #intern | #introductiontostrings | #javastringstutorialforbeginners | #stringpool | #javastringstutorial | #javastringprograminterviewquestionsandanswers | #string | #stringclassinjava | #javastring | #javastrings | #stringinjava | #stringinterning | #stringsinjava | #stringpool | #strings | #introductiontostrings | #javastringtutorial | #stringpoolinjava | #whatisinternmethodinstringclassinjava | #manuallyinternstring | #javastringclasstutorial | #javastringstutorial | #stringinterninginjava | #whystringisimmutableinjava | #javastringpool | #whatisthepurposeofinternmethodinthestringclass
결둠은 intern() λ©”μ„œλ“œλŠ” λ¬Έμžμ—΄ 풀에 μžˆλŠ” 값에 λŒ€ν•΄ equals()을 μˆ˜ν–‰ν•˜κΈ° λ•Œλ¬Έμ— 속도 μΈ‘λ©΄μ—μ„œλŠ” 느릴 μˆ˜λ°–μ— μ—†λ‹€.