β λ³λ ¬ μ²λ¦¬λ?
β SumThread ν΄λμ€ μμ±
class SumThread extends Thread {
private int[] array;
private int start, end;
private int partialSum = 0;
public SumThread(int[] array, int start, int end) {
this.array = array;
this.start = start;
this.end = end;
}
public void run() {
for (int i = start; i < end; i++) {
partialSum += array[i];
}
}
public int getPartialSum() {
return partialSum;
}
}
β λ°°μ΄ μμ± λ° λΆν
int[] numbers = new int[100];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1;
}
int numThreads = 4;
int chunkSize = numbers.length / numThreads;
SumThread[] threads = new SumThread[numThreads];
β joinκ³Ό μ΅μ’ ν©μ°
int totalSum = 0;
try {
for (SumThread thread : threads) {
thread.join();
totalSum += thread.getPartialSum();
}
} catch (InterruptedException e) {
System.out.println("Thread interrupted: " + e.getMessage());
}
System.out.println("Total sum: " + totalSum);
βοΈ ν΅ν©
class SumThread extends Thread {
private int[] array;
private int start, end;
private int partialSum = 0;
public SumThread(int[] array, int start, int end) {
this.array = array;
this.start = start;
this.end = end;
}
public void run() {
for (int i = start; i < end; i++) {
partialSum += array[i];
}
}
public int getPartialSum() {
return partialSum;
}
}
public class Main {
public static void main(String[] args) {
int[] numbers = new int[100];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i;
}
int numThreads = 4; // λ©μ΄λ¦¬ = 4κ°
int chunkSize = numbers.length / numThreads; // ν λ©μ΄λ¦¬ ν¬κΈ° = 25
SumThread[] threads = new SumThread[numThreads]; // λ©μ΄λ¦¬ 4κ° λ€μ΄μλ SumThread[]
for (int i = 0; i < numThreads; i++) {
int start = i * chunkSize;
int end = (i == numThreads - 1) ? numbers.length : start + chunkSize;
// start = 0, 25, 50, 75
// end = 25, 50, 75, 100
threads[i] = new SumThread(numbers, start, end);
threads[i].start();
}
int totalSum = 0;
try {
for (SumThread thread : threads) {
thread.join();
totalSum += thread.getPartialSum();
}
} catch (InterruptedException e) {
System.out.println("Interrupted : " + e.getMessage());
}
System.out.println("Total sum : " + totalSum);
}
}
β λ³λ ¬ μ²λ¦¬μ ν¨κ³Ό
π Thread λΆμ° κ³μ° ꡬ쑰 μμ½
| λ¨κ³ | μ€λͺ |
|---|---|
| λ°°μ΄ μ€λΉ | κ³μ°ν λ°μ΄ν°λ₯Ό λ°°μ΄λ‘ μ€λΉν¨ |
| Thread μμ± | λ°°μ΄μ λλ start~end λ²μλ₯Ό μ€μ ν΄ μ€λ λ μμ± |
| Thread μ€ν | κ° μ€λ λκ° μμ μ΄ λ§‘μ ꡬκ°μ λ³λ ¬λ‘ κ³μ°ν¨ |
| join νΈμΆ | λͺ¨λ μ€λ λκ° μ’ λ£λ λκΉμ§ κΈ°λ€λ¦Ό |
| ν©μ° μ²λ¦¬ | κ° μ€λ λμ κ²°κ³Όλ₯Ό λͺ¨μ μ΅μ’ ν©κ³λ₯Ό κ³μ°ν¨ |
β
μ€λμ ν μ€ μμ½
λ©ν°μ€λ λλ₯Ό μ¬μ©ν λ³λ ¬ μ²λ¦¬λ μ°μ° μλλ₯Ό κ·Ήλνν μ μλ κ°λ ₯ν λꡬμ
λλ€.
β μ΄λ Έν μ΄μ μ΄λ?
β
@Override
β
@Deprecated
β
@SuppressWarnings
β
@FunctionalInterface
β 리νλ μ μ΄λ?
βΉοΈ Introspectionμ μ¬μ μ μλ―Έ : μμμ±μ°° λλ μκΈ°λ°μ±. classλ₯Ό Introspectνλ€λ μλ―Έλ classμ ꡬ쑰λ₯Ό νμ ν΄ properties, methods μ eventsλ₯Ό νμ ν¨μ μλ―Έν©λλ€.
βοΈ νΌλΈλ¦ λ©μλ μ€ν μ€μ΅
import java.lang.reflect.Field;
import java.lang.reflect.Method;
// βλ©ν μ 보λ₯Ό κ°μ Έμ¬ ν
μ€νΈ ν΄λμ€ μμ±
class ReflectionDemo {
public String noSecret = "μλΉλ°μ
λλ€.";
private String secret = "λΉλ°μ
λλ€.";
public ReflectionDemo() {
System.out.println("ReflectionDemo μμ±μ μ€ν");
}
public String greet(String name) {
return "Hello, " + name;
}
private String reveal(String code) {
return "Access granted to: " + code;
}
}
public class Main {
public static void main(String[] args) {
// βClass instanceλ‘ ν΄λΉ ν΄λμ€μ class κ°μ²΄ μ»μ΄μ€κΈ°
Class<?> clazz = ReflectionDemo.class;
// β .getName()μΌλ‘ ν΄λΉ ν΄λμ€μ μ 체 μ΄λ¦ (ν¨ν€μ§κ²½λ‘ ν¬ν¨) νμΈνκΈ°
System.out.println("ν΄λμ€ μ΄λ¦: " + clazz.getName());
// β .getDeclaredFields()λ‘ ν΄λΉ ν΄λμ€μ λͺ¨λ νλ Field[] λ°°μ΄λ‘ κ°μ Έμ€κΈ°
// import java.lang.reflect.Field; μΆκ°
System.out.println("\n[νλ λͺ©λ‘]");
Field[] fields = clazz.getDeclaredFields();
for(Field field: fields) {
System.out.println("νλ: " + field.getName());
}
// β .getDeclaredMethods()λ‘ ν΄λΉ ν΄λμ€μ λͺ¨λ λ©μλ Method[] λ°°μ΄λ‘ κ°μ Έμ€κΈ°
// import java.lang.reflect.Method; μΆκ°
System.out.println("\n[λ©μλ λͺ©λ‘]");
Method[] methods = clazz.getDeclaredMethods();
for(Method method: methods) {
System.out.print("λ©μλ: " + method.getName());
for(Class<?> paramType: method.getParameterTypes()) {
System.out.println(" νλΌλ―Έν° νμ
: " + paramType.getSimpleName());
}
}
try {
// β getDeclaredConstructor().newInstance()λ‘ μμ±μ νΈμΆνμ¬ μΈμ€ν΄μ€ μμ±νκΈ°
Object instance = clazz.getDeclaredConstructor().newInstance();
Method greetMethod = clazz.getDeclaredMethod("greet", String.class);
Object greetResult = greetMethod.invoke(instance, "j797");
System.out.println("\n[νΌλΈλ¦ λ©μλ μ€ν κ²°κ³Ό]");
System.out.println("greet(): " + greetResult);
} catch (Exception e) {
e.printStackTrace();;
}
}
}
βΆοΈ μ€ν κ²°κ³Ό
[νλ λͺ©λ‘]
νλ: noSecret
νλ: secret
[λ©μλ λͺ©λ‘]
λ©μλ: greet νλΌλ―Έν° νμ
: String
λ©μλ: reveal νλΌλ―Έν° νμ
: String
ReflectionDemo μμ±μ μ€ν
[νΌλΈλ¦ λ©μλ μ€ν κ²°κ³Ό]
greet(): Hello, j797
βοΈ νλΌμ΄λΉ λ©μλ μ€ν μμ
Method.invoke(instance, args...)λ₯Ό μ¬μ©νλ©΄ 리νλ μ
μΌλ‘ private λ©μλλ₯Ό μ€νν μ μμ΅λλ€.try {
Object instance = clazz.getDeclaredConstructor().newInstance();
Method revealMethod = clazz.getDeclaredMethod("reveal", String.class);
// βprivate λ©μλλ₯Ό getDeclareMethod("methodName", args
revealMethod.setAccessible(true);
Object revealResult = revealMethod.invoke(instance, "797");
System.out.println("reveal(): " + revealResult);
} catch (Exception e) {
e.printStackTrace();
}
β 리νλ μ κ³Ό κ°μ²΄ μ§ν₯
βοΈ μμ:
Class.forName("ν΄λμ€λͺ
") β μ‘΄μ¬νμ§ μλ ν΄λμ€λ μ»΄νμΌ νμμ νμΈ λΆκ°method.invoke() β μλͺ»λ μΈμ μ λ¬ μ μ»΄νμΌλ¬κ° μ€λ₯λ₯Ό μ‘μ§ λͺ»ν¨π 리νλ μ μ λ¨μ©μ μμ‘΄μ± μ¦κ°μ μ½λ λλ νλ₯Ό μ΄λν μ μμ΅λλ€.
π€ κ·ΈλΌμλ λΆκ΅¬νκ³ λ¦¬νλ μ μ΄ νμν μ΄μ
β AOP (Aspect-Oriented Programming)
λΆκ° λ‘μ§μ μ½μ
ν΄μΌ ν©λλ€.β DI (Dependency Injection), ORM λ±μμμ μ΄λ Έν μ΄μ λΆμ
@Autowired, @Entity λ±μ μ΄λ
Έν
μ΄μ
μ κΈ°λ°μΌλ‘ ν΄λμ€ κ΅¬μ‘°λ₯Ό λΆμνκ³ β νλ¬κ·ΈμΈ ꡬ쑰, λ°νμ κ°μ²΄ 쑰립 λ± μ μ°ν μ€κ³μλ μ μ©
π‘ κ²°λ‘
νλ μμν¬μ μν΄ κ°μ μ μΌλ‘ νμ©λλλ‘ μ€κ³νλ κ²μ΄ κ°μ₯ μ΄μμ μ
λλ€.π 리νλ μ μ£Όμ API μμ½
| λ©μλ | μ€λͺ |
|---|---|
| getName() | ν΄λμ€ μ΄λ¦ λ°ν |
| getDeclaredFields() | λͺ¨λ νλ λ°°μ΄ λ°ν |
| getDeclaredMethods() | λͺ¨λ λ©μλ λ°°μ΄ λ°ν |
| getDeclaredConstructor() | κΈ°λ³Έ μμ±μ λ°ν |
| newInstance() | κ°μ²΄ μΈμ€ν΄μ€ μμ± |
| setAccessible(true) | private λ©€λ² μ κ·Ό νμ© |