Skip to content

Commit

Permalink
add: ๐Ÿ“ฐ[Item 59] ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ตํžˆ๊ณ  ์‚ฌ์šฉํ•˜๋ผ
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinminjin committed Apr 19, 2024
1 parent 07ed25d commit 7c99ef7
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions _posts/effective-java/chapter9/2024-04-20-item59.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
title: Item 59 - ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ตํžˆ๊ณ  ์‚ฌ์šฉํ•˜๋ผ
date: 2024-04-20 00:10:00 +0900
categories: [์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”, chapter9]
tags: [์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”]
---

## **๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋ฏธ์ˆ™ํ•  ๋•Œ ๐Ÿ•ถ๏ธ**

### **๋ฌด์ž‘์œ„ ์ •์ˆ˜ ์ƒ์„ฑ ์˜ˆ์ œ**

```java
public class RandomTest {
// ํ”ํ•˜์ง€๋งŒ ๋ฌธ์ œ๊ฐ€ ์‹ฌ๊ฐํ•œ ์ฝ”๋“œ!
static Random rnd = new Random();
static int random(int n){
return Math.abs(rnd.nextInt()) % n;
}

public static void main(String[] args) {
int n = 2 * (Integer.MAX_VALUE / 3);
int low = 0;
for (int i = 0; i < 1000000; ++i)
if (random(n) < n/2)
low++;
System.out.println(low);
}
}
```
์œ„ ์ฝ”๋“œ๋Š” **์„ธ ๊ฐ€์ง€ ๋ฌธ์ œ**๋ฅผ ๋‚ดํฌํ•˜๊ณ  ์žˆ๋‹ค.

1. n์ด ๊ทธ๋ฆฌ ํฌ์ง€ ์•Š์€ 2์˜ ์ œ๊ณฑ์ˆ˜๋ผ๋ฉด ์–ผ๋งˆ ์ง€๋‚˜์ง€ ์•Š์•„ ๊ฐ™์€ ์ˆ˜์—ด์ด ๋ฐ˜๋ณต๋œ๋‹ค.
2. n์ด 2์˜ ์ œ๊ณฑ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๋ช‡๋ช‡ ์ˆซ์ž๊ฐ€ ํ‰๊ท ์ ์œผ๋กœ ๋” ์ž์ฃผ ๋ฐ˜ํ™˜ ๋œ๋‹ค.
- random ๋ฉ”์„œ๋“œ๊ฐ€ ์ด์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค๋ฉด low์˜ ๊ฐ’์ด ๋Œ€๋žต 50๋งŒ์œผ๋กœ ๋‚˜์™€์•ผ ํ•˜์ง€๋งŒ, 66๋งŒ์— ๊ฐ€๊นŒ์šด ๊ฐ’์„ ์–ป๋Š”๋‹ค. โ†’ 2/3๊ฐ€๋Ÿ‰์ด ์ค‘๊ฐ„๊ฐ’๋ณด๋‹ค ๋‚ฎ์€ ์ชฝ์œผ๋กœ ์ ๋ ค์žˆ๋‹ค.
3. ์ง€์ •ํ•œ ๋ฒ”์œ„ ๋ฐ”๊นฅ์˜ ์ˆ˜๊ฐ€ ํŠ€์–ด๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์ด ์žˆ๋‹ค.
- `Math.abs(Integer.MIN_VALUE)`๋Š” ์ •์ˆ˜ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ํ˜„์ƒ ๋•Œ๋ฌธ์— -2,147,483,648์ด ๋ฐ˜ํ™˜๋œ๋‹ค.
- `rnd.nextInt()`๊ฐ€ `Integer.MIN_VALUE`๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด `Math.abs(rnd.nextInt()) % n`์€ ์Œ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

### **ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• ๐Ÿ”จ**

- ์ด๋ฏธ ์ž˜ ๋งŒ๋“ค์–ด ๋†“์€ `Random.nextInt(int bound)` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ง์ ‘ ํ•ด๊ฒฐํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
- ์ž๋ฐ” 7๋ถ€ํ„ฐ๋Š” `Random` ๋Œ€์‹  `ThreadLocalRandom`์„ ์‚ฌ์šฉํ•˜์ž.
- ํฌํฌ-์กฐ์ธ ํ’€์ด๋‚˜ ๋ณ‘๋ ฌ ์ŠคํŠธ๋ฆผ์—์„œ๋Š” `SplittableRandom`์„ ์‚ฌ์šฉํ•˜์ž.

## **ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ **

- ์ „๋ฌธ๊ฐ€์˜ ์ง€์‹๊ณผ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์˜ ๊ฒฝํ—˜์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
- ํ•ต์‹ฌ์ ์ธ ์ผ๊ณผ ํฌ๊ฒŒ ๊ด€๋ จ ์—†๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š๋ผ ์‹œ๊ฐ„์„ ํ—ˆ๋น„ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค
- ๋”ฐ๋กœ ๋…ธ๋ ฅํ•˜์ง€ ์•Š์•„๋„ ์„ฑ๋Šฅ์ด ์ง€์†ํ•ด์„œ ๊ฐœ์„ ๋œ๋‹ค
- ํŒจ์น˜๋ฅผ ๊ฑฐ๋“ญํ• ์ˆ˜๋ก ๊ธฐ๋Šฅ์ด ์ ์  ๋งŽ์•„์ง„๋‹ค.
- ๋งŽ์€ ๊ฐœ๋ฐœ์ž๋“ค์—๊ฒŒ ๋‚ฏ์ต์€ ์ฝ”๋“œ๊ฐ€ ๋˜์–ด ํ˜‘์—… ํ•˜๊ธฐ ์ข‹๋‹ค.

## **๋ชฐ๋ผ์„œ ๋ชป์“ฐ์ง€๋Š” ๋ง์ž**

- ๋ฉ”์ด์ € ๋ฆด๋ฆฌ์ฆˆ๋งˆ๋‹ค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋งŽ์€ ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋œ๋‹ค.
- ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ฐพ์•„๋ณด์ž.
- [Consolidated JDK 8 Release Notes](https://www.oracle.com/java/technologies/javase/8all-relnotes.html#JSERN108), [Consolidated JDK 11 Release Notes](https://www.oracle.com/java/technologies/javase/11all-relnotes.html#JSERN11)
- [spring.io/blog](https://spring.io/blog)

## **๊ผญ ์•Œ์•„์•ผ ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ**

### **ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ**

- `java.lang`
- `java.util`
- `java.io`
- `Collection framework`
- `Stream`
- `java.util.concurrent`

### **๊ณ ํ’ˆ์งˆ 3rd Party ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ**

- **Guava**: [https://github.com/google/guava](https://github.com/google/guava)

[The Guava library: What are its most useful and/or hidden features?](https://stackoverflow.com/questions/3759440/the-guava-library-what-are-its-most-useful-and-or-hidden-features)

- **Apache Commons Lang**: [https://github.com/apache/commons-lang](https://github.com/apache/commons-lang)

[An Introduction to Apache Commons Lang 3](https://www.baeldung.com/java-commons-lang-3)

[Apache Commons Lang StringUtils - DZone Java](https://dzone.com/articles/apache-commons-lang)

[apache-commons-collections-vs-guava](https://www.baeldung.com/apache-commons-collections-vs-guava)

## **๐Ÿ’ก ํ•ต์‹ฌ ์ •๋ฆฌ**
- ๋ฐ”ํ€ด๋ฅผ ๋‹ค์‹œ ๋ฐœ๋ช…ํ•˜์ง€ ๋ง์ž.
- ์•„์ฃผ ํŠน๋ณ„ํ•œ ๊ธฐ๋Šฅ์ด ์•„๋‹ˆ๋ผ๋ฉด ๋ˆ„๊ตฐ๊ฐ€ ์ด๋ฏธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ํ•ด ๋†“์•˜์„ ๊ฐ€๋Šฅ์„ฑ์ด ํฌ๋‹ค.
- ๊ฐœ๋ฐœํ•˜๊ธฐ๋ณด๋‹ค๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฐพ์•„๋ณด์ž.

0 comments on commit 7c99ef7

Please sign in to comment.