<?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: Rafaela Araújo</title>
    <description>The latest articles on DEV Community by Rafaela Araújo (@rafaaraujoo).</description>
    <link>https://dev.to/rafaaraujoo</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%2F479656%2F67450aff-7191-494c-ba6d-46461090c49f.jpg</url>
      <title>DEV Community: Rafaela Araújo</title>
      <link>https://dev.to/rafaaraujoo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rafaaraujoo"/>
    <language>en</language>
    <item>
      <title>Thread fundamentals in Java</title>
      <dc:creator>Rafaela Araújo</dc:creator>
      <pubDate>Sun, 23 Jun 2024 10:34:21 +0000</pubDate>
      <link>https://dev.to/rafaaraujoo/thread-fundamentals-in-java-43h1</link>
      <guid>https://dev.to/rafaaraujoo/thread-fundamentals-in-java-43h1</guid>
      <description>&lt;h3&gt;
  
  
  What is a thread and why is it important in software development?
&lt;/h3&gt;

&lt;p&gt;A thread is the smallest sequence of instructions that a computer can execute, it's part of a process that can have multiple threads. Every Java program runs in a thread even if you don't create it, the main thread.&lt;br&gt;
Understanding threads is important because we can make full use of the computer resources and write software with good performance, that has lower response time.&lt;br&gt;
But although threads have all these advantages they also bring some complexity when dealing with multithreading programs.&lt;br&gt;
This concept of multithreading was confusing to me, I didn't understand why my program had multiple threads if I hadn't created them?! But then I learned that some frameworks kindly create them for you, if you are from the Spring world it means that every request your application receives runs in a different thread, which means we must create thread-safe programs.&lt;/p&gt;
&lt;h3&gt;
  
  
  And what does it mean thread-safe?
&lt;/h3&gt;

&lt;p&gt;I've read many concepts, in simple words it means that your program must behave predictably when accessed from multiple threads.&lt;br&gt;
Let's say you have this simple class that has a method to increment a counter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class NoSynchronizedCounter {

    public int counter = 0;

    public void incrementCounter() {
        counter++;
    }

    public Integer getCounter() {
        return this.counter;
    }
}

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

&lt;/div&gt;



&lt;p&gt;The expected behavior here is that every time the &lt;code&gt;incrementCounter&lt;/code&gt; is called the counter is incremented by one, in a single-threaded program that will be true, but if this code is called from multiple threads, then we can not guarantee that anymore. Let's execute the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NoSynchronizedCounter simpleCounter = new NoSynchronizedCounter();

Runnable runnable = () -&amp;gt; {
    for (int i=0; i&amp;lt;10_000; i++) {
        simpleCounter.incrementCounter();
    }
};

Thread myThread1 = new Thread(runnable);
Thread myThread2 = new Thread(runnable);
myThread1.start();
myThread2.start();
myThread1.join();
myThread2.join();

System.out.println("Final counter: " + simpleCounter.getCounter());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are calling our &lt;code&gt;incrementCounter&lt;/code&gt; method 10000 times, and running it in two different threads, we expect that the final counter would be 20000, but when I run this code I get a different result &lt;code&gt;14959&lt;/code&gt;, but why does this happen?&lt;br&gt;
In the &lt;code&gt;NoSynchronizedCounter&lt;/code&gt; class, the counter variable is shared between threads, and we can not guarantee that the threads will access the method in order. Two threads can access the &lt;code&gt;incrementCounter&lt;/code&gt; at the same time, if two threads read the same value for example 9, in the end, both threads will increment it to 10 not 11 as would be expected. We lost one increment and the final result will be wrong.&lt;/p&gt;

&lt;p&gt;We need to make this class thread-safe, and we have different ways of doing that. One possible solution would be to change the counter type to AtomicInteger which guarantees that all the operations on counter variable will be atomic. Let's change it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class SynchronizedCounter {

    public AtomicInteger counter = new AtomicInteger(0);

    public void incrementCounter() {
        counter.incrementAndGet();
    }

    public Integer getCounter() {
        return counter.get();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's test it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SynchronizedCounter simpleCounter = new SynchronizedCounter();

Runnable runnable = () -&amp;gt; {
    for (int i=0; i&amp;lt;10_000; i++) {
        simpleCounter.incrementCounter();
    }
};

Thread myThread = new Thread(runnable);
Thread myThread2 = new Thread(runnable);
myThread.start();
myThread2.start();
myThread.join();
myThread2.join();

System.out.println("Final counter: " + simpleCounter.getCounter());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final result here will always be 20000, the AtomicInteger class is part of the Java concurrent package and it works well for this case as we have only one shared state between threads, things get more complicated when there are multiple shared states, but that's a topic for another moment. &lt;/p&gt;

&lt;p&gt;I hope you find this helpful, thanks for reading!&lt;/p&gt;

&lt;h4&gt;
  
  
  References:
&lt;/h4&gt;

&lt;p&gt;Java Concurrency in Practice - Brian Goetz&lt;br&gt;
Effective Java - Joshua Bloch&lt;/p&gt;

</description>
      <category>java</category>
      <category>thread</category>
      <category>concurrency</category>
    </item>
    <item>
      <title>How to setup TinyMCE + React</title>
      <dc:creator>Rafaela Araújo</dc:creator>
      <pubDate>Fri, 02 Oct 2020 18:05:48 +0000</pubDate>
      <link>https://dev.to/rafaaraujoo/how-to-setup-tinymce-react-4aka</link>
      <guid>https://dev.to/rafaaraujoo/how-to-setup-tinymce-react-4aka</guid>
      <description>&lt;p&gt;Recently I had to setup TinyMCE with React in a project. I hope this post could help you. I had some issues trying to do this, and would like to share with you how I did it.&lt;/p&gt;

&lt;p&gt;First of all, you need to download the packages for tinymce and the wrapper for reac:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install tinymce&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm install --save @tinymce/tinymce-react&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And then you are able to start. In order to have TinyMCE self hosted available, you need to import in your React component all the packages that you are going to use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; import React, { useState } from 'react';
 import 'tinymce/tinymce';
 import 'tinymce/icons/default';
 import 'tinymce/themes/silver';
 import 'tinymce/plugins/paste';
 import 'tinymce/plugins/link';
 import 'tinymce/plugins/image';
 import 'tinymce/plugins/table';
 import 'tinymce/skins/ui/oxide/skin.min.css';
 import 'tinymce/skins/ui/oxide/content.min.css';
 import 'tinymce/skins/content/default/content.min.css';
 import { Editor } from '@tinymce/tinymce-react';

 const App = () =&amp;gt; {
   const [contentEditor, setContentEditor] = useState();
   const handleEditorChange = (content, editor) =&amp;gt; {
     console.log('Content was updated:', content);
     setContentEditor(content);
   }

   return (
     &amp;lt;Editor
         initialValue="&amp;lt;p&amp;gt;This is the initial content of the editor&amp;lt;/p&amp;gt;"
         init={{
           skin: false,
           content_css: false,
           height: 500,
           menubar: false,
           plugins: [
             'link image',
             'table paste'
           ],
           toolbar:
             'undo redo | formatselect | bold italic backcolor | \
             alignleft aligncenter alignright alignjustify | \
             bullist numlist outdent indent | removeformat | help'
         }}
         value={contentEditor}
         onEditorChange={this.handleEditorChange}
       /&amp;gt;
     );
   }

 export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the &lt;code&gt;init&lt;/code&gt; prop you have to set &lt;code&gt;skin: false&lt;/code&gt;, and also &lt;code&gt;content_css: false&lt;/code&gt;, so you can get the css you have downloaded from the packages.&lt;/p&gt;

&lt;p&gt;To use the component as a controlled component, use the &lt;code&gt;onEditorChange&lt;/code&gt; prop instead of the &lt;code&gt;onChange&lt;/code&gt; props, and you need to have a state to set the content on it.&lt;/p&gt;

&lt;p&gt;That is it, I hope you find this article useful. Thanks for reading!&lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://www.tiny.cloud/docs/integrations/react/"&gt;https://www.tiny.cloud/docs/integrations/react/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/tinymce/tinymce-react"&gt;https://github.com/tinymce/tinymce-react&lt;/a&gt;&lt;/p&gt;

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