Programming 35

Mutext 뮤텍스 vs Semaphore 세마포어

글에 들어가기에 앞서, 세마포어와 뮤텍스의 가장 기본적인 차이점은 세마포어는 signal mechanism 신호 메커니즘이라는 것이다. 프로세스는 자원을 획득하기 위해 wait()과 signal()을 이용하여 작업을 수행한다. 반면에 뮤텍스는 lock mechanism 잠금 메커니즘이다. 프로세스가 리소스를 획득하려면 해당 뮤텍스 객체의 락을 얻어야한다. 세마포어란? 세마포어는 한국어로 '신호기'라는 의미로서, 마치 기찻길의 신호기처럼 두 개의 철도가 충돌하는 것을 막기위해 두 열차의 진입순서를 막아주는 역할을 한다. Semaphore(int permits, boolean fair) 신호기의 역할은 Semaphore 생성자의 파라미터로 들어가는 int permits이 한다. permits은 세마포어가 가..

[LeetCode] #86 partition List (repeat : 0 )

두시간이나 걸린 문제 .. x보다 작으면 앞에다가 옮기고, x보다 크면 뒤에다가 옮기는 문제다. 노드의 순서는 그대로 유지한 상태여야한다. dummy 노드 두개를 만들어서 x보다 작으면 dummy1에다 넣고 , x보다 크면 dummy2에다 넣은 후 둘이 합치면 된다. #Description Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Examp..

[LeetCode] #9 Remove Nth Node From End of List (repeat:0)

마지막 노드부터 시작하여 인자값으로 주어진 노드를 빼고 head를 리턴하는 문제이다. # Description Given the head of a linked list, remove the nth node from the end of the list and return its head. Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Example 2: Input: head = [1], n = 1 Output: [] Example 3: Input: head = [1,2], n = 1 Output: [1] #Solution class Solution{ public ListNode removeNthFromEnd(ListNode head,int n..

[LeetCode] #141 Linked List Cycle (repeat:1)

이번 문제는 LinkedList에 사이클(Cycle)이 있는지 판단하는 문제이며, 미국 신입개발자들 인터뷰에 많이 나온 문제라고 한다. 이번 문제도 slow와 fast 포인터를 이용하여 쉽게 풀 수 있었다. # Description Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index..

[LeetCode] #876 Middle of the Linked List (repeat:0)

LinkedList의 중간 노드를 찾는 문제인데 미국 신입개발자들 인터뷰에 많이 나온 문제라고 한다. 플로이드의 토끼와 거북이(Floyd Tortoise and Hare) 알고리즘으로 풀었다. # Description Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node. # Example 1 Input: head = [1,2,3,4,5] Output: [3,4,5] Explanation: The middle node of the list is node 3. # Example 2 Input: head =..

[자료구조] 파이썬(Python) VS 자바(Java) 주요 자료구조 비교해보기

파이썬과 자바의 주요 자료구조를 비교해보았다. 비교항목 Array : Java Array VS Python List List : Java ArrayList VS Python List LinkedList : Java LinkedList VS Python Collections.deque Map : Java HashMap VS Python Dictionary Set : Java HashSet VS Python Set 1. Array : Java Array VS Python List Java Array 자바에서 배열(Array)의 크기는 정적(static)이기 때문에 각 요소의 값을 변경하는 것은 가능하지만 크기를 줄이거나 늘릴 수 없다. 처음 선언된 배열의 크기를 변경하는 경우 새로운 메모리 블록이 힙 영역에..

[자료구조] 자바 java.util.LinkedList의 주요 메소드 정리

이 글은 java.util. LinkedList 를 공부하면서 중요하다고 생각되는 메소드를 정리하였다. 1. Constructor public class LinkedList{ private Node head; //첫번째 노드 private Node tail; //마지막 노드 private int length; //노드의 수 class Node{ // Linked List 내부에 Node가 nested 클래스로 들어있음 int value; // 데이터 Node next;// 포인터 Node(int value){ // Node생성자 , 데이터를 파라메터로 받음 this.value = value; } } public LinkedList(int value){ // LinkedList의 생성자 Node newNo..

[Java] 자바 고유락(Intrinsic Lock)이란? (synchronized block)

고유락(Intrinsic Lock) Java에서 고유락(intrinsic lock)은 monitor lock 으로 불리기도 하며, 멀티스레드 환경에서 동기화를 달성하기 위해 사용되는 내부적인 잠금 매커니즘이다. 고유락은 객체당 단 하나만 부여되며, synchronized 키워드를 이용하여 여러 스레드를 동기화 시킬 수 있다. 사용예시는 다음과 같다. public static int counter = 0; // 공유자원 public synchronized void increment(){ // 고유락 counter++; } public static void process(){ Thread t1 = new Thread(new Runnable(){ @Override public void run(){ for(in..

[자료구조] 추상자료형(ADT, Abstract Data Type)과 자료구조(Data Structure)

추상자료형 (ADT, Abstract Data Type) 프로그래밍에서 “superType”이라고 불린다(인터페이스와 추상클래스처럼 파생된 하위형식에 자신의 특성을 제공함) 기본 동작(basic behavior)만을 정의한다(실제로 어떻게 구현되는지에 대한 세부사항은 제외하고 해당 데이터 타입이 제공해야 하는 기본 동작만을 정의한다) 종합해보면 추상자료형(ADT)은 자료구조를 위한 설계도라고 볼 수 있다. 종류 대표적인 추상자료형(ADT)은 스택(STACK)과 큐( QUEUE)를 들 수 있다. 이들은 데이터를 조직화하고 특정 동작만을 수행하기 위한 추상적인 모델을 제공한다. 스택(Stack) Last In, First Out (LIFO) 방식을 따르는 자료구조로, 마지막에 추가된 항목이 가장 먼저 제거되..

[Java] 스레드와 메모리구조 그리고 프로세스(Process)의 구조

메모리의 종류 스택메모리 (Stack Memory) Each JVM thread has a private Java virtual machine stack, created at the same time as the thread. A JVM stack stores frames, also called “stack frames”. A JVM stack is analogous to the stack of a conventional language such as C 각각의 JVM 스레드는 만들어짐과 동시에 JVM스택(Java Virtual Machine Stack ) 이 함께 만들어진다. JVM 스택은 "스택 프레임(Stack Frame)"이라고 하는 프레임들을 저장하며 이는 C언어의 스택 구조와 비슷한 것이다. ..