- 프로세스
👉 메모리에 적재되어 실행되고 있는 프로그램 인스턴스
👉 운영체재로부터 시스템 자원을 할당받은 작업의 단위
👉 프로세스는 독립된 메모리를 할당 받는다
👉 프로세스가 메모리를 관리하기 위해 이 공간들을 어떤 구조로 관리하는데, 이를 프로세스 주소공간이라고 부른다.
1) Code: 코드 자체를 구성하는 메모리 영역( 프로그램 명령 )
2) Data: 전역변수, 정적변수
3) Stack: 자역변수, 험수 매개변수, 리턴 값( 임시 메모리 영역 )
4) Heap: 동적 할당 시 사용 New() , Malloc() 등...
- 스레드
👉 스레드는 프로세스 내의 Code, Data, Heap 영역은 다른 스레드와 공유하고 Stack영역을 따로 할당 받는다.
👉 프로세스와 해당 프로세스 내의 다른 스레드와 자원과 공간을 공유하면서 사용
- 멀티 프로세스
👉 하나의 프로그램을 여러 개의 프로세스로 구성하여 각 프로세스가 하나의 작업을 처리하도록 하는 것
👉 문맥 교환( Context Switching )에서의 오버헤드
👉 CPU는 한번에 하나의 프로세스만 처리할 수 있기 때문에, 여러 프로세스를 처리해야 하는 상황에서는 돌아가면서 여러 프로세스 작업을 처리하는데 이 과정을 문맥교환이라한다.
- 멀티 스레드
👉 하나의 프로그램을 여러 개의 스레드로 구성하고 각 스레드가 하나의 작업을 처리하도록 하는 것
👉 프로세스에 비해 메모리 공간과 시스템 자원 소모가 줄어들게 된다.
- 동기 Vs 비동기
👉 동기: 함수 A와 B를 호출할 때, 함수 A가 함수 B의 리턴 값을 계속 확인하면서 신경 쓰는것
👉 비동기: 함수 A와 함수 B를 호출할 때, 함수 A가 함수 B의 작업 완료 여부는 신경쓰지 않는 것
function cureDisease(human, medicine) // 인자로 받은 medicine으로 human을 치료한다.
function getMedicine() // 치료에 필요한 medicine을 리턴한다.
// 동기
const human = new Human()
const medicine = getMedicine()
cureDisease(human, medicine)
// 비동기
const human = new Human()
console.log('human 병 걸림')
getMedicine({
params: {},
success: function (medicine) {
cureDisease(human, medicine)
}
})
console.log('human 병 치료됨')
$.ajax()
- Java의 비동기
👉 가본적으로 멀티 스레드 방식이다.
👉 JVM에 의해 스레드 스케줄링이 이뤄진다.
👉 Request, DB접근 등..
👉 C와 B가 잡기 게임을한다.
👉 C와 B가 위치가 같아질 때 최소 시간을 구해라
👉 C는 +1 만큼 가고 , B는 -1, +1, *2 만큼간다.
from collections import deque
c = 11
b = 2
def catch_me(cony_loc, brown_loc):
time = 0
queue = deque()
queue.append((brown_loc, 0)) # 위치와 시간을 담아줄게요!.
visited = [{} for _ in range(200001)]
while cony_loc < 200000:
cony_loc += time
if time in visited[cony_loc]:
return time
for i in range(0, len(queue)):
current_position, current_time = queue.popleft()
# 3가지의 선택에 따른 모든 경우의 수를 나열!!
new_position = current_position - 1
new_time = current_time + 1
if new_position >= 0 and new_time not in visited[new_position]:
visited[new_position][new_time] = True
queue.append((new_position, new_time))
new_position = current_position + 1
if new_position < 200001 and new_time not in visited[new_position]:
visited[new_position][new_time] = True
queue.append((new_position, new_time))
new_position = current_position * 2
if new_position < 200001 and new_time not in visited[new_position]:
visited[new_position][new_time] = True
queue.append((new_position, new_time))
time += 1
print(catch_me(c, b))
print("정답 = 3 / 현재 풀이 값 = ", catch_me(10,3))
print("정답 = 8 / 현재 풀이 값 = ", catch_me(51,50))
print("정답 = 28 / 현재 풀이 값 = ", catch_me(550,500))
👉 문자열 뒤집기 문제
- StringBuilder()를 사용한 간결한 코드( 감탄..)
package codingTest;
import java.util.Arrays;
class Solution {
public String solution(String s) {
char [] sol = s.toCharArray();
Arrays.sort(sol);
return new StringBuilder(new String(sol)).reverse().toString();
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.solution("Zbcdefg"));
}
}
- 내가 작성한 코드
package codingTest;
//
public class Solution2 {
public String reversStr(String str){
char[] aStr = str.toCharArray();
char tmp;
int count = aStr.length -1;
while(count > 0){
for(int i=0;i<aStr.length-1;i++){
if(aStr[i] < aStr[i+1]){
tmp = aStr[i];
aStr[i] = aStr[i+1];
aStr[i+1] = tmp;
}
}
count--;
}
return String.valueOf(aStr);
}
public static void main(String[] args) {
Solution2 rs = new Solution2();
System.out.println( rs.reversStr("Zbcdefg") );
}
}
1) 제발 제발 당황하지말고 문제를 천천히 읽자.
2) String은 int나 float같이 자료형으로 쓰이지만 Class이기 때문에, 문자열을 이어주거나 어떤 작업을 할 때 마다 새로운 String Class가 생성되므로 비효율적이다.
따라서 그 떄 StringBuilder를 사용하자!!!!