<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ivinieon</title>
    <description>The latest articles on DEV Community by ivinieon (@ivinieon).</description>
    <link>https://dev.to/ivinieon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1033355%2F71e6f34e-797f-422a-bec9-b61918d6498b.png</url>
      <title>DEV Community: ivinieon</title>
      <link>https://dev.to/ivinieon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ivinieon"/>
    <language>en</language>
    <item>
      <title>Review posting 2</title>
      <dc:creator>ivinieon</dc:creator>
      <pubDate>Mon, 03 Apr 2023 20:32:43 +0000</pubDate>
      <link>https://dev.to/ivinieon/review-posting-2-33a2</link>
      <guid>https://dev.to/ivinieon/review-posting-2-33a2</guid>
      <description>&lt;h2&gt;
  
  
  5. How to decide the number of threads based on CPU bound/IO bound
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Burst&lt;/strong&gt; is a phenomenon that occurs intensively within a short time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CPU burst&lt;/strong&gt; refers to the time a process is continuously executed on the CPU.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IO burst&lt;/strong&gt; refers to the time a process waits for an IO operation to complete after requesting it.&lt;/p&gt;

&lt;p&gt;The life of a process is a continuous sequence of CPU and IO bursts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CPU bound process&lt;/strong&gt;&lt;br&gt;
A process that has many CPU bursts.&lt;br&gt;
ex) video editing software, machine learning programs&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IO bound process&lt;/strong&gt;&lt;br&gt;
A process that has many IO bursts.&lt;br&gt;
ex) typical backend API servers&lt;/p&gt;
&lt;h4&gt;
  
  
  CPU bound 프로그램이 dual-core CPU에서 실행될 때 몇 개의 thread가 사용되어야 할까?
&lt;/h4&gt;

&lt;p&gt;→ Thread 개수  = CPU 개수 +1&lt;br&gt;
너무 많은 thread가 있으면 쓸데없이 context switching이 발생해서 CPU 사용이 낭비되고 늘어나기 때문에.&lt;/p&gt;
&lt;h2&gt;
  
  
  6. Synchronization, Race Condition, Critical Section
&lt;/h2&gt;
&lt;h4&gt;
  
  
  What happens when two threads access one object?
&lt;/h4&gt;

&lt;p&gt;The result can be different depending on when the context switching occurs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Race condition&lt;/strong&gt;: A situation where the result may vary depending on the timing or access order when multiple processes/threads manipulate the same data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Synchronization&lt;/strong&gt;: Maintaining the consistency of shared data even when multiple processes/threads are executed simultaneously.&lt;/p&gt;
&lt;h4&gt;
  
  
  How to synchronize?
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Critical section&lt;/strong&gt;: An area that only one process/thread can enter and execute to ensure the consistency of shared data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conditions for solving the critical section problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Mutual exclusion&lt;/strong&gt;&lt;br&gt;
→ Only one process/thread can execute the critical section at a time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Progress&lt;/strong&gt;&lt;br&gt;
→ If the critical section is empty and multiple processes/threads want to enter the critical section, one of them must be executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Bounded waiting&lt;/strong&gt;&lt;br&gt;
→ Waiting indefinitely to enter the critical section should not be allowed.&lt;/p&gt;
&lt;h2&gt;
  
  
  7. Features and Differences of Spinlock, Mutex, and Semaphore
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;do {
    acquire lock
        critical section
    release lock
        remainder section
} while (TRUE)
-------------------------
volatile int lock = 0; //global

void critical() {
    while (test_and_set(&amp;amp;lock) == 1);
// int TestAndSet(int*lockPtr) {
//     int oldLock = *lockPtr;
//     *lockPtr = 1;
//     return oldLock;
}
    ... critical section
    lock = 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;두 개의 thread가 있다고 가정할 때, T1이 while loop에 먼저 들어간다. &lt;/li&gt;
&lt;li&gt;test_and_set(&amp;amp;lock) 함수에서, lock의 value인 0은 oldLock에 저장되고 새롭게 1이라는 value를 가지게 된다.&lt;/li&gt;
&lt;li&gt;그리고 이 함수는 oldLock, 즉 0을 리턴하게 되면서 while이 false이기 때문에  T1은 while을 종료하고 critical section으로 진입한다. &lt;/li&gt;
&lt;li&gt;T2가 이때 while에 접근하지만, test_and_set(&amp;amp;lock)은 계속해서 1을 리턴하기 때문에 true라서 while에 갇히게 된다.&lt;/li&gt;
&lt;li&gt;T1이 critical section에서 나오면 lock은 0이 되고 그 때 T2는 while에서 나오게 되어 critical section에 진입하게 된다.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  What if both enter the while loop at the same time?
&lt;/h4&gt;

&lt;p&gt;CPU가 오직 하나만 들어올 수 있게 해준다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;이 방법은 CPU를 낭비하게 된다.&lt;/strong&gt;&lt;br&gt;
Spinlock: Repeatedly try until you can have the lock (while loop).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Mutex {
        int value = 1;
        int guard = 0;
}
------------------
Mutex::lock() {
        while(test_and_set(&amp;amp;guard));
        if(value == 0) {
             ...현재 스레드를 큐에 넣음;
             guard = 0; &amp;amp; go to sleep
        } else {
             value = 0;
             guard = 0;
        }
}
--------------------
Mutex::unlock(){
    while(test_and_set(&amp;amp;guard));
    if(큐에 하나라도 대기중이라면){
            그 중에 하나를 깨운다;
    } else {
            value = 1;
  }
    guard = 0;
}
---------------
mutex -&amp;gt; lock();
...critical section
mutex -&amp;gt; unlock();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Critical section에 들어가기 위해 경쟁할 때, lock이 실행된다.&lt;/li&gt;
&lt;li&gt;value가 0일 때, 현재 thread를 queue에 넣는다.&lt;/li&gt;
&lt;li&gt;unlock이 queue에 하나라도 대기중이라면 그 중 하나를 깨운다.
&amp;amp;arr; CPU 사용율를 줄일 수 있음&lt;/li&gt;
&lt;li&gt;guard는 여러 개의 process나 thread가 접근할 때 value를 보호하는 장치이다.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Is a mutex always better than a spinlock?
&lt;/h4&gt;

&lt;p&gt;If the multi-core environment and the operation in the critical section are completed faster than context switching, then spinlock has more advantages than mutex.&lt;br&gt;
Context switching occurs during sleeping and awakening in mutex.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Semaphore&lt;/strong&gt;: A device that allows one or more processes/threads to access the critical section using signal mechanisms.&lt;br&gt;
The semaphore consists of an integer variable that can have a value of 0 or higher.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Semaphore {
        int value = 1; //0,1,2,3 이 될 수도 있음
        int guard = 0;
}
------------------
Semaphore::wait() {
        while(test_and_set(&amp;amp;guard));
        if(value == 0) {
             ...현재 스레드를 큐에 넣음;
             guard = 0; &amp;amp; go to sleep
        } else {
             value -= 1;
             guard = 0;
        }
}
--------------------
Semaphore::signal(){
    while(test_and_set(&amp;amp;guard));
    if(큐에 하나라도 대기중이라면){
            그 중에 하나를 깨운다;
    } else {
            value += 1;
  }
    guard = 0;
}
---------------
semaphore -&amp;gt; wait();
...critical section
semaphore -&amp;gt; signal();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Multi-core environment&lt;/strong&gt;&lt;br&gt;
Multi-core environment에서 value가 0으로 세팅되어 있을 때, P1과 P2는 동시에 실행되기 위해 할당되고 task3는 task1 다음에 실행되게 한다.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;만약 P1이 signal을 먼저 호출하고 value가 1이 되면, P2는 wait를 호출하고 value를 0으로 만든다. 그런 다음 task3가 실행된다.&lt;/li&gt;
&lt;li&gt;만약 P2가 먼저 wait를 호출하고 value가 여전히 0이면, queue에 들어가게 된다. P1이 signal을 호출하고 나면 value는 1이 되고 P2는 wait에서 나올 수 있게 되고 task3이 실행된다.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;mutex와 binary의 차이점&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mutex에서는 오직 lock을 가지고 있는 것만 lock을 해제 할 수 있지만 semaphore에서는 그렇지 않다. 하지만 semaphore에서는 wait와 signal이 꼭 같은 프로세스, 스레드에서 실행될 필요가 없다.&lt;/li&gt;
&lt;li&gt;Mutex는 priority inheritance 속성을 가진다. 여러 프로세스나 스레드가 동시에 실행이 되면 우선순위에 따라 실행시키게 되는데 이를 스케줄링이라고 한다.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>backend</category>
    </item>
    <item>
      <title>Review posting</title>
      <dc:creator>ivinieon</dc:creator>
      <pubDate>Mon, 20 Mar 2023 20:29:43 +0000</pubDate>
      <link>https://dev.to/ivinieon/review-posting-1fhm</link>
      <guid>https://dev.to/ivinieon/review-posting-1fhm</guid>
      <description>&lt;h2&gt;
  
  
  1. Basic coding words
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Object&lt;/strong&gt; has states and behaviors.&lt;br&gt;
&lt;strong&gt;Class&lt;/strong&gt; describes the attributes and behaviors of the object.&lt;br&gt;
instantiate: creating an object using the "new" keyword.&lt;br&gt;
(Class는 새로운 object를 만드는 것이고 그 만드는 것을 instantiate라고 한다.)&lt;br&gt;
Class 안에는 attributes, methods 들이 있고, 이 class에서 만든 object들은 이 attributes들과 methods를 가진다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;primitive type&lt;/strong&gt; is predefined by the language such as int or boolean. If the Class creates a new object, it has the class type which was defined in the Class.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Relationship between Variables, Objects, and Memory
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Application&lt;/strong&gt; is a set of commands that a computer can execute to provide functions for the users.&lt;br&gt;
&lt;strong&gt;Memory&lt;/strong&gt; is the place where the executed application resides. Memory allocated to the application is internally divided into several areas.&lt;br&gt;
→&lt;strong&gt;Identifier&lt;/strong&gt;: name given to entity(이름)&lt;br&gt;
→&lt;strong&gt;Variable&lt;/strong&gt;: name given to location(실제 메모리 위치)&lt;br&gt;
&lt;strong&gt;CPU&lt;/strong&gt; is the computational unit that executes commands.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stack memory&lt;/strong&gt;&lt;br&gt;
Whenever a function or method is called, a stack frame is stacked in the stack memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Heap memory&lt;/strong&gt;&lt;br&gt;
Heap memory is used during the creation of an object, and stack memory is also used for it. However, in the stack memory, there is "this" which points to the heap memory's address.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. How can multiple programs be executed?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Process&lt;/strong&gt; is a program that is running on a computer. Each process is assigned an independent memory space.&lt;br&gt;
&lt;strong&gt;Thread&lt;/strong&gt; is a unit of execution. A process can have more than one thread.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Multi-tasking&lt;/strong&gt;: The multiple processes and threads are sharing a core and executing very short CPU time slices alternatively.&lt;br&gt;
&lt;strong&gt;Multi-processing&lt;/strong&gt;: A system that utilizes two or more processors or cores.&lt;br&gt;
&lt;strong&gt;Multi threading&lt;/strong&gt;: A single process executes multiple tasks(dual threads) simultaneously. Each core executes one task in parallel.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. What is context switching?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Context Switching&lt;/strong&gt;&lt;br&gt;
The process of replacing a running process/thread with another process/thread on a CPU/core.&lt;br&gt;
&lt;strong&gt;- Process context switching&lt;/strong&gt;&lt;br&gt;
→ additional processing related to virtual memory addresses is performed. MMU TLB&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Thread context switching&lt;/strong&gt;&lt;br&gt;
→ memory-related processing is not needed because memory is shared within the same process&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>backend</category>
    </item>
    <item>
      <title>Concepts and Use Cases of Stack and Queue</title>
      <dc:creator>ivinieon</dc:creator>
      <pubDate>Mon, 20 Mar 2023 04:42:05 +0000</pubDate>
      <link>https://dev.to/ivinieon/concepts-and-use-cases-of-stack-and-queue-5c9m</link>
      <guid>https://dev.to/ivinieon/concepts-and-use-cases-of-stack-and-queue-5c9m</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;1. Concepts and Use Cases of Stack and Queue&lt;/strong&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;2. Tips When Encountering a Queue in Technical Documents&lt;/strong&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;3. Troubleshooting Errors Related to Stack and Queue&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Abstract Data Type (ADT)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Defines only what operations should be performed without specifying how they should be implemented&lt;/li&gt;
&lt;li&gt;Does not cover implementation details&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Data structure (DS)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A concrete implementation of the ADT&lt;/li&gt;
&lt;li&gt;Implements the operations defined in the ADT&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Stack and Queue from the perspective of ADT
&lt;/h2&gt;

&lt;p&gt;Stack: A structure that stores data in Last In First Out (LIFO) order&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;push: Adds an item to the stack&lt;/li&gt;
&lt;li&gt;pop: Removes an item from the stack&lt;/li&gt;
&lt;li&gt;peek: Retrieves the value of the topmost item in the stack&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since the item that is inserted first is placed at the bottom of the stack, the item that is inserted last is the first to be removed.&lt;/p&gt;

&lt;p&gt;Queue: A structure that stores data in First In First Out (FIFO) order&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;enqueue: Adds an item to the queue&lt;/li&gt;
&lt;li&gt;dequeue: Removes an item from the queue&lt;/li&gt;
&lt;li&gt;peek: Retrieves the value of the frontmost item in the queue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The item that is inserted first is the first to be removed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Cases for Stack: Stack Memory and Stack Frame&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python
Copy code
def a():
        b()
def b():
        c()
def c():
        print("wow")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a() is called, a stack for a() is created in the stack memory. Since b() is called inside a(), a stack for b() is also created. Similarly, when b() is called, a stack for c() is created. After the "wow" is printed, c() is removed from the stack, followed by b(), and finally a().&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Cases for Queue: Producer/Consumer Architecture&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The producer creates and the consumer consumes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tips When Encountering a Queue in Technical Documents
&lt;/h2&gt;

&lt;p&gt;FIFO is not always implied: In a priority queue, items with higher priority are removed first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stack/Queue Related Errors and Troubleshooting Methods
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;StackOverflowError&lt;/strong&gt; &lt;br&gt;
An error that occurs when the stack memory is exhausted. This error occurs when a recursive function cannot exit.&lt;/p&gt;

&lt;p&gt;Recursive Function: Calling a function within itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python
Copy code
def recur_fibo(n):
    if n&amp;lt;=1:
        return n
    else:
        return(Recur_fino(n-1)+recur_fibo(n-2))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;OutOfMemoryError&lt;/strong&gt;&lt;br&gt;
 An error that occurs when the Java heap memory is exhausted. This error occurs when the queue is constantly being filled with data.&lt;/p&gt;

&lt;p&gt;Heap: The area where objects reside.&lt;/p&gt;

&lt;p&gt;The queue size should be fixed. What if the queue is full?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Throw an exception&lt;/li&gt;
&lt;li&gt;Return a special value (null or false)&lt;/li&gt;
&lt;li&gt;Block the thread indefinitely until success&lt;/li&gt;
&lt;li&gt;Block the thread for a limited amount of time and then give up&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;LinkedBlockingQueue&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AkdsvUmq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhw26x9llzkuuxfbfcet.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AkdsvUmq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhw26x9llzkuuxfbfcet.png" alt="Image description" width="674" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This posting is just a study note which was written after watching youtube videos in Korean.&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://youtu.be/-2YpvLCT5F8"&gt;Youtube&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>backend</category>
    </item>
    <item>
      <title>How is the monitor used for synchronization?</title>
      <dc:creator>ivinieon</dc:creator>
      <pubDate>Thu, 16 Mar 2023 04:23:07 +0000</pubDate>
      <link>https://dev.to/ivinieon/how-is-the-monitor-used-for-synchronization-dcc</link>
      <guid>https://dev.to/ivinieon/how-is-the-monitor-used-for-synchronization-dcc</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;Monitor&lt;/strong&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Ensures mutual exclusion&lt;/li&gt;
&lt;li&gt;Allows threads to wait based on conditions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When is it used?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When only one thread should be executing at a time&lt;/li&gt;
&lt;li&gt;When collaboration between multiple threads is needed&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Components of the monitor
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;- Mutex&lt;/strong&gt;: A device that ensures mutual exclusion in critical sections. A thread must acquire a mutex lock to enter a critical section. If a thread cannot acquire the mutex lock, it goes into a waiting state in the queue. When a thread holding the mutex lock releases it, one of the waiting threads in the queue is selected to execute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Condition variables&lt;/strong&gt;: Have a waiting queue. Where threads wait for a condition to be met in a waiting state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Operations on condition variables:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;wait: The thread puts itself in the waiting queue of the condition variable and goes into a waiting state.&lt;/li&gt;
&lt;li&gt;signal: Wakes up one of the threads waiting in the waiting queue.&lt;/li&gt;
&lt;li&gt;broadcast: Wakes up all threads waiting in the waiting queue.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;acquire(m) // Acquire the lock of the monitor
while(!p) { // Check the condition
    wait(m, cv); // If the condition is not met, wait
}
...
signal(cv2); --OR-- broadcast(cv2); //cv2 can be the same as cv
release(m); // Release the lock of the monitor

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When a thread enters the waiting queue, it must release the lock.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Two queues
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;- Entry queue:&lt;/strong&gt; The queue of threads waiting &lt;strong&gt;to enter the critical section.&lt;/strong&gt; Managed by the mutex.&lt;br&gt;
&lt;strong&gt;- Waiting queue:&lt;/strong&gt; The queue of threads waiting for a &lt;strong&gt;condition&lt;/strong&gt; to be met. Managed by the condition variable.&lt;/p&gt;
&lt;h3&gt;
  
  
  Bounded producer/consumer problem:
&lt;/h3&gt;

&lt;p&gt;The producer creates goods and fills them in a buffer, while the consumer consumes the goods.&lt;/p&gt;

&lt;p&gt;Problem scenario 1: The producer keeps creating goods, but the buffer is full and there is no room left.&lt;/p&gt;

&lt;p&gt;Problem scenario 2: The consumer tries to consume goods, but the buffer is empty and there is nothing to consume.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;global volatile Buffer q;
global Lock lock;
global CV fullCV;
global CV emptyCV;
-------------------------------------------------------
public method producer() {
    while(true) {
        task myTask = ...'

        lock.acquire(); 
        // A mutex lock is required to enter the CV. If there is no lock, enter the entry queue and wait.

        while(q.isFull()) {
            wait(lock, fullCV); // Check if the buffer is full. If it is full, wait in the waiting queue.
        }

        q.enqueue(myTask);

        signal(emptyCV); --or-- broadcast(emptyCV);

        lock.release();
        }
    }

--------------------------------------------------------

public method consumer() {
    while(true) {
        lock.acquire();
        // The producer and consumer compete for one lock.
        while(q.isEmpty()) {{
            wait(lock, emptyCV);
        } // Always wait in the while loop so that when awakened, it can check if the condition is met.
            // If the buffer is empty, wait in the waiting queue.
        myTask = q.dequeue();

        signal(fullCV); --or-- broadcast(fullCV);

        lock.release();

        doStuff(myTask);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What is a Java monitor?
&lt;/h3&gt;

&lt;p&gt;In Java, every object has an internal monitor.&lt;/p&gt;

&lt;p&gt;The mutual exclusion functionality of the monitor is used with the synchronized keyword.&lt;/p&gt;

&lt;p&gt;Java monitors have only one condition variable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Three operations of the Java monitor:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;wait&lt;/li&gt;
&lt;li&gt;notify(signal)&lt;/li&gt;
&lt;li&gt;notifyAll(broadcast)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;This posting is just a study note which was written after watching youtube videos in Korean.&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://dev.toyoutube%20link"&gt; https://youtu.be/Dms1oBmRAlo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>backend</category>
      <category>synchronization</category>
    </item>
    <item>
      <title>Features and Differences of Spinlock, Mutex, and Semaphore</title>
      <dc:creator>ivinieon</dc:creator>
      <pubDate>Fri, 10 Mar 2023 21:45:33 +0000</pubDate>
      <link>https://dev.to/ivinieon/features-and-differences-of-spinlock-mutex-and-semaphore-3l1m</link>
      <guid>https://dev.to/ivinieon/features-and-differences-of-spinlock-mutex-and-semaphore-3l1m</guid>
      <description>&lt;h3&gt;
  
  
  Review
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Race condition&lt;/strong&gt;: A situation where multiple processes/threads manipulate the same data at the same time, and the result can vary depending on the timing or access order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Synchronization&lt;/strong&gt;: Maintaining the consistency of shared data even when multiple processes/threads are executed simultaneously.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Critical section&lt;/strong&gt;: An area that can only be entered and executed by one process/thread to ensure the consistency of shared data.&lt;/p&gt;

&lt;p&gt;→ &lt;strong&gt;Mutual exclusion&lt;/strong&gt;: Allowing only one process/thread to enter and execute.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;How can we ensure mutual exclusion?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;→ Let's use locks!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;do {
    acquire lock
        critical section
    release lock
        remainder section
} while (TRUE)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;volatile int lock = 0; //global

void critical() {
    while (test_and_set(&amp;amp;lock) == 1);
// int TestAndSet(int*lockPtr) {
//     int oldLock = *lockPtr;
//     *lockPtr = 1;
//     return oldLock;
    ... critical section
    lock = 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Process:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Assuming there are two threads, T1 and T2, and T1 executes the while loop first.&lt;/li&gt;
&lt;li&gt;In the test_and_set(&amp;amp;lock) function, the value of lock(lockPtr), which is 0, is stored in oldLock, and lock(lockPtr) is changed to 1.&lt;/li&gt;
&lt;li&gt;Since this function returns lodLock, which is 0, the while loop becomes false, and T1 exits the while loop and enters the critical section.&lt;/li&gt;
&lt;li&gt;At this point, T2 executes, and enters the while loop (T1 is still in the critical section). Since the value of lock is 1, test_and_set(&amp;amp;lock) returns 1.&lt;/li&gt;
&lt;li&gt;When you look inside the function, lock(lockPtr) has a value of 1, and this value is stored in oldLock. Then, lock(lockPtr) remains 1 and oldLock, which is 1, is returned.
T6. herefore, the while loop is true, and T2 continues to run the while loop.&lt;/li&gt;
&lt;li&gt;After that, when T1 exits the critical section and the value of lock changes to 0, T2 inside the while loop can exit and enter the critical section.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;→ In other words, simultaneous execution is not possible.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What if both enter the while loop at the same time?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The TestAndSet function uses the help of the CPU.&lt;/p&gt;

&lt;p&gt;→ CPU atomic instructions&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They are not interfered with or interrupted during execution.&lt;/li&gt;
&lt;li&gt;They are not executed simultaneously for the same memory area.
→ Even if two or more processes/threads access it, the CPU allows only one to come. It synchronizes two processes so that they cannot run simultaneously.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: The TestAndSet function does not necessarily have to have that body, but it proceeds in such a form.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Spinlock&lt;/strong&gt;: Repeatedly try until you can have the lock (while loop).&lt;/p&gt;

&lt;p&gt;→ Waste CPU while waiting.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Mutex {
        int value = 1;
        int guard = 0;
}
------------------
Mutex::lock() {
        while(test_and_set(&amp;amp;guard));
        if(value == 0) {
             ...현재 스레드를 큐에 넣음;
             guard = 0; &amp;amp; go to sleep
        } else {
             value = 0;
             guard = 0;
        }
}
--------------------
Mutex::unlock(){
    while(test_and_set(&amp;amp;guard));
    if(큐에 하나라도 대기중이라면){
            그 중에 하나를 깨운다;
    } else {
            value = 1;
  }
    guard = 0;
}
---------------
mutex -&amp;gt; lock();
...critical section
mutex -&amp;gt; unlock();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Process:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When competing to access the critical section, a lock is executed based on whether or not the value can have a value.&lt;/li&gt;
&lt;li&gt;If the value cannot have a value (value == 0), it goes into a queue and later one of the threads in the queue is awakened in the unlock phase.
→ This minimizes unnecessary CPU cycles.&lt;/li&gt;
&lt;li&gt;The guard is a device installed in test_and_set to protect the value in case multiple processes/threads access it (an atomic device at the CPU level).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Mutex:&lt;/strong&gt; Rest until you can have the lock.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Is a mutex always better than a spinlock?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If the multi-core environment and the operation in the critical section are completed faster than context switching, then spinlock has more advantages than mutex.&lt;/p&gt;

&lt;p&gt;Context switching occurs during sleeping and awakening in mutex.&lt;/p&gt;

&lt;p&gt;In a CPU with two cores, if T1 is executing in C1's critical section and T2 continues to spinlock in C2, T2 can execute immediately when T1 exits the critical section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Semaphore:&lt;/strong&gt; A device that allows one or more processes/threads to access the critical section using signal mechanisms.&lt;/p&gt;

&lt;p&gt;A semaphore is a synchronization tool that allows one or more processes or threads to access the critical section. The semaphore consists of an integer variable that can have a value of 0 or higher.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Semaphore {
        int value = 1; //0,1,2,3 이 될 수도 있음
        int guard = 0;
}
------------------
Semaphore::wait() {
        while(test_and_set(&amp;amp;guard));
        if(value == 0) {
             ...현재 스레드를 큐에 넣음;
             guard = 0; &amp;amp; go to sleep
        } else {
             value -= 1;
             guard = 0;
        }
}
--------------------
Semaphore::signal(){
    while(test_and_set(&amp;amp;guard));
    if(큐에 하나라도 대기중이라면){
            그 중에 하나를 깨운다;
    } else {
            value += 1;
  }
    guard = 0;
}
---------------
semaphore -&amp;gt; wait();
...critical section
semaphore -&amp;gt; signal();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the class Semaphore, the value determines how many threads/processes can run simultaneously. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;value = 1; binary semaphore.&lt;br&gt;
When Semaphore::wait is called and value is not 0, value -= 1 is executed, and value becomes 0. Subsequently, threads/processes after that point will enter a queue because value is 0.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When value is set to 2 or more, counting semaphore &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;signal mechanisms : for order&lt;/p&gt;

&lt;p&gt;In a multi-core environment where value is set to 0, P1 and P2 are allocated to run simultaneously, and task3 is scheduled to run after task1 finishes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If P1 calls signal first, value becomes 1, P2 calls wait, and value becomes 0. Then, task3 can start running.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If P2 calls wait first, value is still 0, so it enters the queue in the if statement. When P1 finishes calling signal and value becomes 1, P2 can exit wait and task3 can start running.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;→ Therefore, in Semaphore, it is not necessary for wait and signal to be executed in the same process/thread.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The difference between mutex and binary&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Semaphore is that only the lock holder can release the lock in mutex, while in semaphore, it is not necessary. However, in semaphore, it is not necessary for wait and signal to be executed in the same process/thread. &lt;/li&gt;
&lt;li&gt;Mutex has the property of priority inheritance. When multiple processes/threads are executed at the same time, the scheduling is done based on priority. 
→ If P2 has a lower priority than P1 and P2 holds the lock and executes first, P1 has to wait until P2 finishes. This slows down the speed of leaving the critical section, and P1 continues to wait. To solve this, P2's priority can be increased to match P1's, allowing for faster exit from the critical section.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If only mutual exclusion is required, it is recommended to use mutex. If synchronization of execution order between tasks is required, it is recommended to use semaphore.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This posting is just a study note which was written after watching youtube videos in Korean.&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://dev.toyoutube%20link"&gt; https://youtu.be/gTkvX2Awj6g&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>backend</category>
    </item>
    <item>
      <title>Synchronization, Race Condition, Critical Section</title>
      <dc:creator>ivinieon</dc:creator>
      <pubDate>Tue, 07 Mar 2023 05:06:14 +0000</pubDate>
      <link>https://dev.to/ivinieon/synchronization-race-condition-critical-section-d69</link>
      <guid>https://dev.to/ivinieon/synchronization-race-condition-critical-section-d69</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;What happens when two threads access one object?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;→ Why is synchronization important?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;java
Copy code
for(apple for appleBox) {
    if(apple status is bad) {
        badCounter.increment(); 
    }
}

public class Counter {
    private int state = 0;
    public void increment() {state++;}
//1. LOAD state to R1 // Load state into R1
//2. R1 = R1 + 1 // Add 1 to R1 and store the result in R1
//3. STORE R1 to state // Store R1 back into state
    public int get() {return state;}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Assuming that two threads check for bad apples in one appleBox respectively using this code, if T1 finds 2 bad apples and T2 finds 5 bad apples, the value of memory's state, which is initially 0, is expected to be 7, but that's not always the case.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The result can be different depending on when the context switching occurs.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;For example, when T1 loads the state from memory to R1, R1 becomes 0. Then, when T1 stores the result of adding 1 to R1 in R1, R1 becomes 1. If a context switching occurs at this point, T2 executes steps 1 and 2, and stores the value of R1 in memory's state. Then, when context switching occurs again and T1 stores R1 in state, the value remains 1 because T2 already stored it as 1. (It should have been 2 originally.)&lt;br&gt;
→Race condition&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Race condition&lt;/strong&gt;: A situation where the result may vary depending on the timing or access order when multiple processes/threads manipulate the same data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Synchronization&lt;/strong&gt;: Maintaining the consistency of shared data even when multiple processes/threads are executed simultaneously.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;How to synchronize?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Critical section&lt;/strong&gt;: An area that only one process/thread can enter and execute to ensure the consistency of shared data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;java
Copy code
do{
    entry section
        critical section
    exit section
        reminder section
} while(TRUE)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conditions for solving the critical section problem&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Mutual exclusion&lt;/strong&gt;&lt;br&gt;
→ Only one process/thread can execute the critical section at a time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Progress&lt;/strong&gt;&lt;br&gt;
→ If the critical section is empty and multiple processes/threads want to enter the critical section, one of them must be executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Bounded waiting&lt;/strong&gt;&lt;br&gt;
→ Waiting indefinitely to enter the critical section should not be allowed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This posting is just a study note which was written after watching youtube videos in Korean.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/vp0Gckz3z64"&gt;Youtube Link&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to decide the number of threads based on CPU bound/ IO bound</title>
      <dc:creator>ivinieon</dc:creator>
      <pubDate>Sat, 04 Mar 2023 23:04:25 +0000</pubDate>
      <link>https://dev.to/ivinieon/how-to-decide-the-number-of-threads-based-on-cpu-bound-io-bound-35o4</link>
      <guid>https://dev.to/ivinieon/how-to-decide-the-number-of-threads-based-on-cpu-bound-io-bound-35o4</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;How to decide the number of threads based on CPU bound/ IO bound&lt;/strong&gt;
&lt;/h1&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Review/Preview words&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;CPU(Central Processing Unit)&lt;/strong&gt; is the device that interprets and executes the commands of a process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IO(Input/Output)&lt;/strong&gt; involves reading and writing files, sending and receiving data with a network, and interacting with input/output devices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Burst&lt;/strong&gt; is a phenomenon that occurs intensively within a short time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CPU burst&lt;/strong&gt; refers to the time a process is continuously executed on the CPU.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IO burst&lt;/strong&gt; refers to the time a process waits for an IO operation to complete after requesting it.&lt;/p&gt;

&lt;p&gt;The life of a process is a continuous sequence of CPU and IO bursts.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The frequency of CPU bursts depends on their length.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjv8l05kw12v8ck1urv3p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjv8l05kw12v8ck1urv3p.png" alt=" " width="533" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CPU bound process&lt;/strong&gt;&lt;br&gt;
A process that has many CPU bursts.&lt;br&gt;
ex) video editing software, machine learning programs&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IO bound process&lt;/strong&gt;&lt;br&gt;
A process that has many IO bursts.&lt;br&gt;
ex) typical backend API servers&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;If implementing a CPU bound program to run on a dual-core CPU, how many threads should be used?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;→ The number of threads = The number of CPUs + 1&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm4rgm52h563cwbiuilc0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm4rgm52h563cwbiuilc0.png" alt=" " width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In actual execution:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkhkmcmpxonrue2n8wmxa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkhkmcmpxonrue2n8wmxa.png" alt=" " width="505" height="180"&gt;&lt;/a&gt;&lt;br&gt;
→ Context switching(CS) is overhead, tasks that use the CPU.&lt;br&gt;
In this picture, unnecessary context-switching occurs, the CPU usage increases. &lt;/p&gt;

&lt;p&gt;Dual-core &amp;amp; 2 threads:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxa5nfbj922ickb7fn2l4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxa5nfbj922ickb7fn2l4.png" alt=" " width="800" height="366"&gt;&lt;/a&gt;&lt;br&gt;
Since there is no context switching, CPU usage is not wasted, and the appropriate number of threads is one more than the number of CPU cores.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;If implementing an IO bound program to run on a dual-core CPU, how many threads should be used?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;→ It depends on the situation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the API server is a thread-per-request model?
→ The number of threads to be created in advance should be determined by considering various situations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;This posting is just a study note which was written after watching youtube videos in Korean.&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://youtu.be/qnVKEwjG_gM" rel="noopener noreferrer"&gt;Youtube link&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ci</category>
      <category>cicd</category>
      <category>documentation</category>
      <category>discuss</category>
    </item>
    <item>
      <title>What is a context switching?</title>
      <dc:creator>ivinieon</dc:creator>
      <pubDate>Thu, 02 Mar 2023 06:03:22 +0000</pubDate>
      <link>https://dev.to/ivinieon/what-is-a-context-switching-45o6</link>
      <guid>https://dev.to/ivinieon/what-is-a-context-switching-45o6</guid>
      <description>&lt;h1&gt;
  
  
  What is context switching?
&lt;/h1&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Context Switching&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The process of replacing a running process/thread with another process/thread on a CPU/core.&lt;br&gt;
Each process has a thread, which is the basic unit executed on a CPU/core.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a context?
&lt;/h3&gt;

&lt;p&gt;The status of a process/thread, CPU, memory, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is context switching necessary?
&lt;/h3&gt;

&lt;p&gt;To execute multiple processes/threads simultaneously.&lt;/p&gt;

&lt;h3&gt;
  
  
  When does context switching occur?
&lt;/h3&gt;

&lt;p&gt;When the given time slice(quantum) has been used up, when IO operations are required, when using other resources, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Context switching in a multitasking system
&lt;/h2&gt;

&lt;p&gt;→ Since the program is executed by dividing it into short periods, the user feels like programs are being executed simultaneously.&lt;/p&gt;

&lt;h3&gt;
  
  
  Who executes context switching?
&lt;/h3&gt;

&lt;p&gt;OK kurnel: manages/supervises various resources.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does context switching occur?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Process context switching: switching between different processes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thread context switching: switching between threads in the same process.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Commonalities:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Executed in kernel mode&lt;br&gt;
→ control is transferred from the process to the kernel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Registers of the CPU are replaced&lt;br&gt;
→ the register state of the process being executed is saved somewhere, and another process is executed. Later, the saved register state of the first process is used to execute it again.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Registers: a location where data necessary to execute instructions is stored.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Differences:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Process context switching&lt;br&gt;
→ additional processing related to virtual memory addresses is performed. MMU TLB&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thread context switching&lt;br&gt;
→ memory-related processing is not needed because memory is shared within the same process.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Memory processing must be performed to prevent incorrect access to each other's memory areas.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thread context switching is faster than process context switching.(sharing a memory)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Indirect effects of context switching: cache pollution.&lt;br&gt;
Since the previous thread's information may be stored in the cache, when the thread is switched, it becomes useless.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From the user's perspective: pure overhead.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;This posting is just a study note which was written after watching youtube videos in Korean.&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://youtu.be/qnVKEwjG_gM"&gt;Youtube Link&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How can multiple programs be executed?</title>
      <dc:creator>ivinieon</dc:creator>
      <pubDate>Mon, 27 Feb 2023 21:00:32 +0000</pubDate>
      <link>https://dev.to/ivinieon/how-can-multiple-programs-be-executed-2elb</link>
      <guid>https://dev.to/ivinieon/how-can-multiple-programs-be-executed-2elb</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;How can multiple programs be executed?&lt;/strong&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Program: A collection of commands that a computer can execute.&lt;/li&gt;
&lt;li&gt;Process: A program that is running on a computer. Each process is assigned an independent memory space.&lt;/li&gt;
&lt;li&gt;CPU: The computational unit that executes commands.&lt;/li&gt;
&lt;li&gt;Main Memory:  Where processes wait to be executed by the CPU.&lt;/li&gt;
&lt;li&gt;I/O(Input/Output): Reading and writing files, sending and receiving data somewhere on the network. Handling input and output devices and sending or receiving data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Single process system
&lt;/h2&gt;

&lt;p&gt;Only one program runs at a time. CPU usage is not efficient because when a program performs an IO operation, the CPU is idle.&lt;br&gt;
→ Load multiple programs into memory and execute them simultaneously. When IO operation occurs, another process runs on the CPU:&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiprogramming
&lt;/h2&gt;

&lt;p&gt;To maximize CPU usage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages&lt;/strong&gt;&lt;br&gt;
If the CPU usage time is long, the processes continue to wait.&lt;br&gt;
→ Let the process run for a very short time(quantum) when using the CPU. Alternatively executing very short periods of time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multitasking
&lt;/h2&gt;

&lt;p&gt;To minimize the response time of the process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A single process cannot perform multiple tasks simultaneously. &lt;/li&gt;
&lt;li&gt;Context switching between processes is a heavy task(programs cross over.)&lt;/li&gt;
&lt;li&gt;Data sharing between processes is difficult.
→ the memory space they occupy is independent.&lt;/li&gt;
&lt;li&gt;Dual-core processors have been released.
&amp;amp;raar; improving the performance of a single CPU has trouble such as overheating.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Thread
&lt;/h3&gt;

&lt;p&gt;A process can have more than one thread.&lt;br&gt;
Unit of execution(CPU).&lt;br&gt;
Context switching between threads of the same process is lightweight.&lt;br&gt;
Threads share the memory space of the process they belong to.&lt;br&gt;
→ Threads also have. their unique areas as well.(such as stacks)&lt;/p&gt;

&lt;h2&gt;
  
  
  Multithreading
&lt;/h2&gt;

&lt;p&gt;A single process executes multiple tasks simultaneously.&lt;br&gt;
Each core executes one task in parallel.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extended multitasking concept: Multiple processes and threads share very short CPU time slices.
&lt;/h3&gt;

&lt;h2&gt;
  
  
  Multiprocessing
&lt;/h2&gt;

&lt;p&gt;A system that utilizes two or more processors or cores.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;br&gt;
Multitasking: Sharing cores&lt;br&gt;
Multithreading: Dual Threads&lt;br&gt;
Multiprocessing: More than 2 cores.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This posting is just a study note which was written after watching youtube videos in Korean.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/QmtYKZC0lMU"&gt;Youtube Link&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Relationship between Variables, Objects, and Memory</title>
      <dc:creator>ivinieon</dc:creator>
      <pubDate>Sat, 25 Feb 2023 22:41:45 +0000</pubDate>
      <link>https://dev.to/ivinieon/relationship-between-variables-objects-and-memory-5bbk</link>
      <guid>https://dev.to/ivinieon/relationship-between-variables-objects-and-memory-5bbk</guid>
      <description>&lt;h1&gt;
  
  
  Relationship between Variables, Objects, and Memory
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;What is an Application&lt;br&gt;
→ A set of commands that a computer can execute to provide functions for general users.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is a Memory?&lt;br&gt;
→ The place where the executed application resides. Memory allocated to the application is internally divided into several areas. &lt;br&gt;
&lt;strong&gt;An identifier is a “name given to entity” in a program whereas, a variable is a “name given to memory location”.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is a CPU?&lt;br&gt;
→ The hardware component of a computer performs most of the processing tasks, executing instructions or commands that are stored in memory based on the instructions it receives from software.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;## Stack Memory&lt;/strong&gt;&lt;br&gt;
Local variables and parameters of functions or methods are stored. A stack frame is stacked each time a function or method is called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;wow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the main(String[] args) method is called, a stack frame for main() is created in the stack memory, and each parameter (args) and the value (100) for each local variable are stored, and a name (a) is attached.&lt;/p&gt;

&lt;p&gt;When wow(a) is called, a stack frame for wow() is created again in the stack memory, and the parameter int num is stored in it, and since num = a, num becomes 100, b becomes 400, and that value is returned. Since the wow method call is over, the wow stack frame will be deleted, and the returned 400 will become the value of a.&lt;br&gt;
In addition, if this stack memory is full, the application will terminate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;## Heap Memory&lt;/strong&gt;&lt;br&gt;
Objects are stored.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Counter&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;tow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;two&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Counter&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;increment&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;increment&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="o"&gt;++;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="o"&gt;;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;args is a parameter.&lt;/li&gt;
&lt;li&gt;c is a local variable.&lt;/li&gt;
&lt;li&gt;state is an instance variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the main() method is called, a stack frame for main() is created in the stack, and the args parameter and variable c are stored. c is a variable of Counter type, and when this class is created, i.e., when an object is created, it will be stored in the heap memory. Of course, a stack frame for the Counter() method is also created in the stack, and a variable called "this" is created inside it, and the value of this stores the address of the data stored in the heap memory. After the object creation is complete, the Counter stack frame in the stack disappears.&lt;/p&gt;

&lt;p&gt;Then, the two(c) method is called, and a stack frame for two() is created in the stack memory, and c as a parameter is stored in it. This method calls the increment() method of c, and another stack frame is created for this, and state becomes 1. Then, another increment is called, and a stack frame is created for it, and state becomes 2, and then it disappears.&lt;/p&gt;

&lt;p&gt;The variable count creates a stack frame for calling c's get() method, so it retrieves the state value (2). Therefore, the count value in the main method's stack frame becomes 2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;## Garbage Object&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Counter&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;make&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt; &lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Counter&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;&lt;span class="c1"&gt;//Object creation&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Counter&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;//Creating a new object again&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="o"&gt;++;}&lt;/span&gt;
        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="o"&gt;;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The make() method creates two objects. Since an object has been created, the two objects are stored in the heap memory. However, the object that variable c points to is the second object created by the return. Therefore, there is no way to access the first object created. Thus, it wastes the heap memory. Depending on the language, there is a Garbage Collector that cleans up and removes these objects. This is usually referred to as GC.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This posting is just a study note which was written after watching youtube videos in Korean.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/GIsr_r8XztQ"&gt;Youtube Link&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Basic coding words</title>
      <dc:creator>ivinieon</dc:creator>
      <pubDate>Fri, 24 Feb 2023 21:41:18 +0000</pubDate>
      <link>https://dev.to/ivinieon/basic-coding-words-34no</link>
      <guid>https://dev.to/ivinieon/basic-coding-words-34no</guid>
      <description>&lt;h1&gt;
  
  
  Basic coding words
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;1. What is an Object?&lt;/strong&gt; &lt;br&gt;
→ An entity that has states and behaviors.&lt;/p&gt;

&lt;p&gt;Example of an Object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
person1 = Person("John", 30)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, Person is a class that represents a person. An object person1 is created from the class with the name "John" and age 30.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. What is a Class?&lt;/strong&gt; &lt;br&gt;
→ A blueprint that describes the attributes and behavior of an entity. It is not an object, but objects are created from it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;instantiate : creating an object using the "new" keyword&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example of a Class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    private String name;
    private double speed;
    private Size size;

    public void start() { ... }
    public void stop() { ... }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This class represents a car and has three attributes: name, speed, and size. It also has two methods: start() and stop(), which represent the behaviors of a car.&lt;/p&gt;

&lt;p&gt;Next, we create three Car objects using the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Car myCar = new Car("niro");
Car yourCar = new Car("sonata");
Car ourCar = new Car("spotage");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These lines of code create three Car objects named myCar, yourCar, and ourCar, respectively. We use the new keyword to instantiate these objects, and we pass in a string argument to set the name attribute of each object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. What is a Variable?&lt;/strong&gt;&lt;br&gt;
→ A named container that holds a value. The value can be changed.&lt;/p&gt;

&lt;p&gt;Example of a Variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int age = 36;
Boolean success = true;
Car myCar = new Car("niro");
String channelName = "easyCode";

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We define four variables named age, success, myCar, and channelName. The first two variables are of the primitive types int and Boolean, respectively. The third variable is of the Car class type, and we instantiate a new Car object and assign it to this variable. The fourth variable is of the String class type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. What is a Function?&lt;/strong&gt;&lt;br&gt;
→ A set of independent code that performs a task. It can be called by its name, and may or not may receive parameters or return values. It is resuable.&lt;/p&gt;

&lt;p&gt;Example of a Function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;age = 37;
myCar = new Car("bmw");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We change the value of the age variable from 36 to 37. We also create a new Car object with the name "bmw" and assign it to the myCar variable, effectively replacing the previous Car object that was assigned to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. What is a Method?&lt;/strong&gt;&lt;br&gt;
→ A set of code that performs a task and is dependent on a class or object. It can access the state of the object or class.&lt;/p&gt;

&lt;p&gt;Example of a Method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Counter {
    private int count = 0;

    public void increment() {
        count++;
    }

    public int get() {
        return count;
    }
}

Counter c1 = new Counter(); // create an object first
c1.increment(); // call the increment method twice
c1.increment();
int result1 = c1.get(); // call the get method to get the result

Counter c2 = new Counter();
c2.increment();
int result2 = c2.get();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This class represents a counter and has one attribute count and two methods increment() and get(). The increment() method increments the count attribute by 1, and the get() method returns the current value of the count attribute.&lt;/p&gt;

&lt;p&gt;We then create two Counter objects named c1 and c2, respectively. The c1 calls the increment() method twice and then calls the get() method and the c2 calls the increment() method once and then calls the get() method. The values of result1 should be 2, and result2 should be 1, respectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This posting is just a study note which was written after watching youtube videos in Korean.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/2bL2mVXGr4I" rel="noopener noreferrer"&gt;Youtube video what I watched&lt;/a&gt;&lt;/p&gt;

</description>
      <category>welcome</category>
    </item>
  </channel>
</rss>
