<?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: vishalpaalakurthi</title>
    <description>The latest articles on DEV Community by vishalpaalakurthi (@vishalpaalakurthi).</description>
    <link>https://dev.to/vishalpaalakurthi</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F82029%2F309d8b84-d377-432a-a622-433dfb8cad97.jpeg</url>
      <title>DEV Community: vishalpaalakurthi</title>
      <link>https://dev.to/vishalpaalakurthi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vishalpaalakurthi"/>
    <language>en</language>
    <item>
      <title>3 Prompt Mistakes That Wreck Your AI Output (and the One-Message Fix)</title>
      <dc:creator>vishalpaalakurthi</dc:creator>
      <pubDate>Fri, 26 Jun 2026 15:24:47 +0000</pubDate>
      <link>https://dev.to/vishalpaalakurthi/3-prompt-mistakes-that-wreck-your-ai-output-and-the-one-message-fix-3m58</link>
      <guid>https://dev.to/vishalpaalakurthi/3-prompt-mistakes-that-wreck-your-ai-output-and-the-one-message-fix-3m58</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Your AI keeps giving you mush. The prompt isn't bad. It's broken in three specific places.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you've ever typed a question into an AI chat, read the answer, and thought &lt;em&gt;"this is technically correct and yet completely useless,"&lt;/em&gt; you're not alone. The instinct is to blame the model — newer, smarter, more expensive. But almost every time, the model isn't the bottleneck.&lt;/p&gt;

&lt;p&gt;The prompt is.&lt;/p&gt;

&lt;p&gt;And the prompt is broken in the same three ways for almost everyone.&lt;/p&gt;




&lt;h2&gt;
  
  
  The new-hire test
&lt;/h2&gt;

&lt;p&gt;Picture training a new hire. If you said &lt;em&gt;"do good work"&lt;/em&gt; and walked away, you'd get nothing useful back. Not because they're bad at their job. Because you gave them no audience, no goal, no examples, no shape.&lt;/p&gt;

&lt;p&gt;Most prompts read exactly like &lt;em&gt;"do good work."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Generic instructions. No examples. No clear shape. And then we're surprised when the output is generic.&lt;/p&gt;

&lt;p&gt;The fix is unglamorous: three small changes to how you write the request. None of them require a new model, a paid tier, or a course on "prompt engineering." Five minutes. Different output forever.&lt;/p&gt;




&lt;h2&gt;
  
  
  🎥 Prefer to watch this?
&lt;/h2&gt;

&lt;p&gt;If you'd rather have this delivered as a three-minute video — same lesson, calmer pace, on the &lt;em&gt;Learn in 3&lt;/em&gt; channel — here it is:&lt;/p&gt;

&lt;p&gt;📺 &lt;strong&gt;Watch on YouTube:&lt;/strong&gt; &lt;a href="https://youtu.be/k46xNVNfeo4?si=9EibkW511JG7L6fd" rel="noopener noreferrer"&gt;3 Prompt Mistakes That Wreck Your AI Prompts&lt;/a&gt;&lt;br&gt;
  &lt;iframe src="https://www.youtube.com/embed/k46xNVNfeo4"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Otherwise keep reading. The text version below is the whole thing, end to end.&lt;/p&gt;




&lt;h2&gt;
  
  
  Mistake 1 — The vague ask
&lt;/h2&gt;

&lt;p&gt;The most common mistake, by a wide margin.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"Write me a blog post."&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That isn't a prompt. It's a wish. The AI doesn't know your audience, your length, your tone, or your goal — so it averages everything and hands you the middle. Bland marketing copy. Cold and forgettable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix is unglamorous.&lt;/strong&gt; Add the audience. Add the length. Add the tone.&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Write me a blog post.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Write a 600-word blog post for marketing managers,
in a friendly editorial tone, on prompt engineering basics.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Specifics aren't extra. &lt;strong&gt;Specifics are the prompt.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The difference is bigger than you'd expect — go try the two versions side by side, on whichever AI chat you use.&lt;/p&gt;




&lt;h2&gt;
  
  
  Mistake 2 — No role assigned
&lt;/h2&gt;

&lt;p&gt;Asking the AI a question without telling it &lt;em&gt;who to be&lt;/em&gt; is asking a stranger on the street. You'll get an answer, but it'll be the median answer.&lt;/p&gt;

&lt;p&gt;Tell it to answer as a marketing director. Or a kindergarten teacher. Or a friendly editor. Or — if you're a developer — a senior Python engineer reviewing a junior's PR.&lt;/p&gt;

&lt;p&gt;Same question, different expert, completely different answer.&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How do I make this function faster?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;You&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;re a senior Python engineer doing a code review.
The function below is O(n²). Rewrite it for O(n) where possible,
and explain the change in two sentences.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The role unlocks the model's reservoir of "what would this kind of expert say." That reservoir is huge. It just sits dormant until you turn it on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pick the role first. Then ask.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Mistake 3 — No examples
&lt;/h2&gt;

&lt;p&gt;You're asking the AI to match your style without ever showing it your style. That's guessing.&lt;/p&gt;

&lt;p&gt;Paste two or three short samples of the kind of output you want. The AI copies the pattern. This trick is called &lt;strong&gt;few-shot prompting&lt;/strong&gt;, and three examples beats a paragraph of instructions every time.&lt;/p&gt;

&lt;p&gt;Three is the sweet spot. One looks like a coincidence. Two leaves wiggle room. Three creates a clear pattern the model locks onto.&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Write a product description in our style.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Write a product description in our voice.

Here are three of ours we like:
1. "Cedar-aged. Quietly defiant."
2. "Hand-bound. Mistakes welcome."
3. "Loud in the right places."

Now write one for a leather-bound notebook.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll watch the style transfer happen in real time. Sentence length, vocabulary, rhythm — all of it.&lt;/p&gt;

&lt;p&gt;The trap most people fall into is picking three examples that contradict each other. One formal, one casual, one short, one rambling. The AI gets confused and gives you mush.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Choose three examples that share the same voice and structure.&lt;/strong&gt; Consistency is the lesson.&lt;/p&gt;




&lt;h2&gt;
  
  
  The fix, all in one message
&lt;/h2&gt;

&lt;p&gt;Here's what it looks like when you stack all three together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You are a friendly editor.
Rewrite this paragraph for a tired Tuesday-morning reader,
in under 80 words.

Here are three short rewrites I've loved:
1. [example one]
2. [example two]
3. [example three]

Here's the paragraph: [...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One message. A role. A goal. A constraint. Three examples.&lt;/p&gt;

&lt;p&gt;The AI has almost nothing left to guess. The output sharpens immediately — usually on the first try.&lt;/p&gt;

&lt;p&gt;That's it. That's the whole framework.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try it in the next five minutes
&lt;/h2&gt;

&lt;p&gt;Don't bookmark this. The lesson dies in the saved tabs.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your last AI chat.&lt;/li&gt;
&lt;li&gt;Pick a prompt that disappointed you.&lt;/li&gt;
&lt;li&gt;Rewrite it with &lt;strong&gt;a role, a clear goal, and three examples.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Send it again.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's the experiment. Five minutes, max.&lt;/p&gt;

&lt;p&gt;If the output sharpens — and it will — leave the before-and-after in the comments. I read all of them and reply with the next thing to tighten.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published as part of Learn in 3 — one idea, broken into three, every week. → &lt;a href="https://youtube.com/@learn-in-3?si=Obvcj0Jz9nps1PMs" rel="noopener noreferrer"&gt;https://youtube.com/@learn-in-3&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>promptengineering</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Learn in 3: Introducing the Channel</title>
      <dc:creator>vishalpaalakurthi</dc:creator>
      <pubDate>Tue, 23 Jun 2026 16:37:09 +0000</pubDate>
      <link>https://dev.to/vishalpaalakurthi/learn-in-3-introducing-the-channel-20a2</link>
      <guid>https://dev.to/vishalpaalakurthi/learn-in-3-introducing-the-channel-20a2</guid>
      <description>&lt;p&gt;One idea. Always three. Every week. Short, clear videos that break big topics into three pieces you can actually use.&lt;/p&gt;

&lt;p&gt;Learn in 3 turns big topics into three pieces you can actually use.&lt;br&gt;
Every video takes one idea — AI, money, productivity, history, science, whatever you've been meaning to figure out — and breaks it into three. Three steps. Three rules. Three mistakes. Three minutes. Whatever shape fits the idea best.&lt;br&gt;
No fluff. No jargon. No twenty-minute videos that could have been five.&lt;br&gt;
New episode every week. Subscribe if you'd rather learn than scroll.&lt;br&gt;
📩 Got a topic you want broken into three? Drop it in the comments of any video.&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/Xz1WFaJxnCw"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;If you like the content, please subscribe to the channel and stay tuned for more updates.&lt;br&gt;
&lt;a href="https://youtube.com/@learn-in-3?si=VSjtC4PUxHBZ_AgL" rel="noopener noreferrer"&gt;Learn in 3&lt;/a&gt;&lt;/p&gt;

</description>
      <category>learning</category>
      <category>youtube</category>
      <category>socialmedia</category>
      <category>community</category>
    </item>
    <item>
      <title>The One Prompt Engineering Trick That Actually Works</title>
      <dc:creator>vishalpaalakurthi</dc:creator>
      <pubDate>Tue, 23 Jun 2026 15:49:40 +0000</pubDate>
      <link>https://dev.to/vishalpaalakurthi/the-one-prompt-engineering-trick-that-actually-works-41e5</link>
      <guid>https://dev.to/vishalpaalakurthi/the-one-prompt-engineering-trick-that-actually-works-41e5</guid>
      <description>&lt;p&gt;Your prompts are fine. Your AI output is still garbage.&lt;/p&gt;

&lt;p&gt;You write carefully. You're specific. You ask for the format, the tone, the length. Hit enter. The AI responds with something that sounds like it was written by a committee of lawyers having a really bad day.&lt;/p&gt;

&lt;p&gt;Here's what you don't realize: &lt;strong&gt;You're not telling the AI to do something. You're describing the problem, and the AI is solving for the statistical average.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The fix isn't more detailed instructions. It's three examples.&lt;/p&gt;

&lt;p&gt;That's it. Three. Not ten, not one, three.&lt;/p&gt;

&lt;p&gt;This post is the complete guide to &lt;strong&gt;few-shot prompting&lt;/strong&gt; — the single highest-leverage move in prompt engineering. By the end, you'll have a template you can copy into any AI and watch your output quality jump 5x.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Prefer watching? Here's the &lt;a href="https://youtu.be/HiU2YEIs_ew?si=te_DjgG8wCKjTiW6" rel="noopener noreferrer"&gt;3-minute version&lt;/a&gt;&lt;br&gt;
  &lt;iframe src="https://www.youtube.com/embed/HiU2YEIs_ew"&gt;
  &lt;/iframe&gt;
 &lt;br&gt;
Otherwise, read on — everything's below.&lt;/em&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Why Instructions Fail (And Examples Work)
&lt;/h2&gt;

&lt;p&gt;When you tell an AI to "be funny," it's working off a fuzzy statistical average of everything labeled "funny" in its training data.&lt;/p&gt;

&lt;p&gt;When you &lt;em&gt;show&lt;/em&gt; an AI what you think is funny, you're giving it a precise pattern to match.&lt;/p&gt;

&lt;p&gt;Here's the difference:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;❌ Instruction:&lt;/strong&gt; "Write a funny one-sentence movie summary"&lt;/p&gt;

&lt;p&gt;Result: A lukewarm joke that lands in the middle of the comedy bell curve.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Pattern:&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;Funny summary of The Lion King: Cub loses dad. Cub becomes king.
Funny summary of Finding Nemo: Dad fish swims very far for his son.
Funny summary of Titanic: [AI fills this in]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Boy meets girl. Boat meets iceberg. Oops.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Same AI. Different universe. The only thing that changed: you showed it the pattern instead of describing it.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Science (Why This Isn't Magic)
&lt;/h2&gt;

&lt;p&gt;Language models predict the next token by pattern matching. They've seen millions of prompt-response pairs and learned: "When a prompt looks like &lt;em&gt;this&lt;/em&gt;, the output usually looks like &lt;em&gt;that&lt;/em&gt;."&lt;/p&gt;

&lt;p&gt;One example could be a fluke.&lt;br&gt;&lt;br&gt;
Two examples might be a coincidence.&lt;br&gt;&lt;br&gt;
Three examples are clearly a pattern.&lt;/p&gt;

&lt;p&gt;The AI recognizes the pattern and completes it.&lt;/p&gt;

&lt;p&gt;This is exactly how humans learn. We don't learn to write by reading style guides. We learn by reading examples of good writing. We internalize the pattern and reproduce it.&lt;/p&gt;

&lt;p&gt;Few-shot prompting weaponizes this principle. You're not asking the AI to understand abstract rules. You're showing it a shape and asking it to complete the shape.&lt;/p&gt;


&lt;h2&gt;
  
  
  Your Complete Few-Shot Template
&lt;/h2&gt;

&lt;p&gt;Here's what you paste into ChatGPT, Claude, or Gemini:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I want you to [your task].

Here are three examples of exactly what I'm looking for:

Example 1:
Input: [sample input]
Output: [sample output in your desired style]

Example 2:
Input: [sample input]
Output: [sample output in your desired style]

Example 3:
Input: [sample input]
Output: [sample output in your desired style]

Now apply this pattern to:
Input: [your real task]
Output:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's literally the entire framework. Copy this structure. Fill in your examples. Run it once.&lt;/p&gt;

&lt;p&gt;The difference is instant.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Three Rules That Make Examples Work
&lt;/h2&gt;

&lt;p&gt;Not all examples are created equal. Here's what actually moves the needle:&lt;/p&gt;

&lt;h3&gt;
  
  
  Rule 1: Keep Them Short
&lt;/h3&gt;

&lt;p&gt;Your examples teach through their shape. A two-word example signals "brevity." A ten-word example signals "detail."&lt;/p&gt;

&lt;p&gt;If your examples are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product: Pen
Product: Ultra-Premium Chair Featuring Ergonomic Excellence
Product: Lamp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The AI doesn't know which to follow. It sees wildly inconsistent shapes and guesses.&lt;/p&gt;

&lt;p&gt;If they're:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product: Pen → Name: Ink Flow
Product: Chair → Name: Sit Comfort  
Product: Lamp → Name: Light Beam
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All the same shape. The AI recognizes it instantly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rule 2: Make Them Consistent With Each Other
&lt;/h3&gt;

&lt;p&gt;Format matters. Length matters. Capitalization matters.&lt;/p&gt;

&lt;p&gt;Bad:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product: Water bottle → Name: H2O Container
Product: Coffee mug → Name: The Morning Brew Machine
Product: Thermos → Name: Hot
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product: Water bottle → Name: Hydration Buddy
Product: Coffee mug → Name: Wake-Up Pal
Product: Thermos → Name: Heat Holder
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All follow the same pattern: [Noun] + [Personality-adjective]. The AI learns this formula and applies it consistently.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rule 3: Examples Must Look Like Your Real Task
&lt;/h3&gt;

&lt;p&gt;If you want luxury product names, don't use discount brands as examples. If you want technical jargon, don't use casual slang. The AI learns the domain too.&lt;/p&gt;

&lt;p&gt;Examples are blueprints. Build them with precision.&lt;/p&gt;




&lt;h2&gt;
  
  
  The One Mistake That Kills Everything
&lt;/h2&gt;

&lt;p&gt;The AI copies your examples exactly. Typos and all.&lt;/p&gt;

&lt;p&gt;If your example has a typo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product: Water bottle → Name: Hydration Buddie [typo]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you ask for "backpack," you might get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product: Backpack → Name: Portagge Palle [picking up typo patterns]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Spend 30 seconds cleaning your examples before you use them.&lt;/strong&gt; Check for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Typos&lt;/li&gt;
&lt;li&gt;Inconsistent capitalization
&lt;/li&gt;
&lt;li&gt;Spacing issues&lt;/li&gt;
&lt;li&gt;Missing punctuation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One clean pass prevents the AI from learning bad patterns.&lt;/p&gt;




&lt;h2&gt;
  
  
  When Few-Shot Is Worth It (And When It Isn't)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Use few-shot when you care about:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Voice/tone&lt;/strong&gt; — Sales emails that sound like you, not a template&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Format&lt;/strong&gt; — Tables, JSON, bullet lists, specific output structures&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Naming&lt;/strong&gt; — Products, variables, chapter titles, anything you want consistent&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Style&lt;/strong&gt; — How casual, how technical, how brief&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Skip few-shot when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Factual questions&lt;/strong&gt; — "What year was Python released?" doesn't need style examples&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Novel problems&lt;/strong&gt; — Something genuinely new where you have no examples to show&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complex reasoning&lt;/strong&gt; — Chain-of-thought prompts usually outperform few-shot&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most day-to-day AI work falls into the "voice/format" bucket. This is where few-shot wins.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try It Tonight
&lt;/h2&gt;

&lt;p&gt;Pick one thing you keep redoing with AI. Write three examples right now. Takes 2 minutes.&lt;/p&gt;

&lt;p&gt;Then use the template.&lt;/p&gt;

&lt;p&gt;See what happens.&lt;/p&gt;

&lt;p&gt;I'll bet the quality jump surprises you.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Full Experience
&lt;/h2&gt;

&lt;p&gt;I made a 3-minute video that walks through this with live examples — movie summaries, product naming, the exact prompts you can copy. Plus the one warning that ruins everything if you miss it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://youtu.be/HiU2YEIs_ew?si=te_DjgG8wCKjTiW6" rel="noopener noreferrer"&gt;Watch the video →&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
if you want more on prompt engineering that actually moves the needle.&lt;/p&gt;




&lt;h3&gt;
  
  
  Share This
&lt;/h3&gt;

&lt;p&gt;If this helped, share it with someone doing AI work. Few-shot prompting is criminally underrated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Questions?&lt;/strong&gt; Drop them in the comments — I reply to every substantive one.&lt;/p&gt;

</description>
      <category>promptengineering</category>
      <category>chatgpt</category>
      <category>ai</category>
      <category>prompting</category>
    </item>
    <item>
      <title>Exposing Hazelcast Metrics with JMX and Prometheus</title>
      <dc:creator>vishalpaalakurthi</dc:creator>
      <pubDate>Fri, 20 Sep 2024 11:11:20 +0000</pubDate>
      <link>https://dev.to/vishalpaalakurthi/exposing-hazelcast-metrics-with-jmx-and-prometheus-3k35</link>
      <guid>https://dev.to/vishalpaalakurthi/exposing-hazelcast-metrics-with-jmx-and-prometheus-3k35</guid>
      <description>&lt;p&gt;Monitoring and managing the performance of your distributed systems is crucial, especially when using a data grid like Hazelcast. One of the effective ways to expose metrics from Hazelcast is by utilizing the JMX (Java Management Extensions) Prometheus exporter. This blog will guide you through the steps to set up the JMX Prometheus exporter to expose Hazelcast metrics for monitoring.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Download the JMX Prometheus Exporter
&lt;/h2&gt;

&lt;p&gt;First, you need to download the JMX Prometheus Java agent. This can be easily obtained from the Maven repository. Use the following link to download the JAR file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/1.0.1/jmx_prometheus_javaagent-1.0.1.jar" rel="noopener noreferrer"&gt;Download jmx_prometheus_javaagent-1.0.1.jar&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make sure to save the JAR file in a convenient location on your server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Create the JMX Exporter Configuration File
&lt;/h2&gt;

&lt;p&gt;Next, you'll need to create a configuration file named &lt;code&gt;jmx_exporter.yml&lt;/code&gt;. This file will specify which metrics to collect from the Hazelcast instance. Below is an example configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;startDelaySeconds&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
&lt;span class="na"&gt;attrNameSnakeCase&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="na"&gt;lowercaseOutputName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="na"&gt;lowercaseOutputLabelNames&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="na"&gt;whitelistObjectNames&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;com.hazelcast:type=Metrics,*"&lt;/span&gt;
&lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;pattern&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;^com.hazelcast&amp;lt;type=Metrics,&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;instance=(.*),&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;prefix=(.*),&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;tag([0-9]+)=(.*)&amp;gt;&amp;lt;&amp;gt;(.+):"&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;hazelcast_$5&lt;/span&gt;
    &lt;span class="na"&gt;labels&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;instance&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;$1&lt;/span&gt;
      &lt;span class="na"&gt;prefix&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;$2&lt;/span&gt;
      &lt;span class="na"&gt;tag$3&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;$4&lt;/span&gt;

  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;pattern&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;^com.hazelcast&amp;lt;type=Metrics,&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;instance=(.*),&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;prefix=(.*)&amp;gt;&amp;lt;&amp;gt;(.+):"&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;hazelcast_$3&lt;/span&gt;
    &lt;span class="na"&gt;labels&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;instance&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;$1&lt;/span&gt;
      &lt;span class="na"&gt;prefix&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;$2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Important Note:
&lt;/h3&gt;

&lt;p&gt;When running the JMX exporter as a Java agent, do not configure the &lt;code&gt;jmxUrl&lt;/code&gt; or &lt;code&gt;hostPort&lt;/code&gt;. This is because you’re not monitoring a remote JVM; you’re monitoring the local Hazelcast instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Enable Metrics in Hazelcast
&lt;/h2&gt;

&lt;p&gt;Before starting your Hazelcast application, you must enable metrics in its configuration. This can be done by modifying the Java code as follows:&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="nc"&gt;Config&lt;/span&gt; &lt;span class="n"&gt;config&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;Config&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hazelcast.jmx"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hazelcast.metrics.enabled"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code snippet ensures that JMX and metrics are enabled within your Hazelcast configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Start the Hazelcast Application with JMX Exporter
&lt;/h2&gt;

&lt;p&gt;Now it's time to start your Hazelcast application with the JMX Prometheus exporter. Use the following command to run your application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;java &lt;span class="nt"&gt;-javaagent&lt;/span&gt;:&amp;lt;path&amp;gt;/jmx_prometheus_javaagent-1.0.1.jar&lt;span class="o"&gt;=&lt;/span&gt;8080:&amp;lt;path&amp;gt;/jmx_exporter.yml &lt;span class="nt"&gt;-cp&lt;/span&gt; &lt;span class="s1"&gt;'common-model-1.9.0-SNAPSHOT.jar:lib/*'&lt;/span&gt; io.dxchange.resource.efs.HazelcastServer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Note:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Replace &lt;code&gt;&amp;lt;path&amp;gt;&lt;/code&gt; with the actual path to your JAR and configuration file.&lt;/li&gt;
&lt;li&gt;You can change the &lt;code&gt;8080&lt;/code&gt; port to any available port if needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 5: Verify the Metrics
&lt;/h2&gt;

&lt;p&gt;To ensure everything is set up correctly, you can verify the exposed metrics using the following &lt;code&gt;curl&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl http://localhost:8080/metrics
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will return a list of metrics that have been collected from your Hazelcast instance. You should see a response similar to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# HELP hazelcast_some_metric Description of some metric
# TYPE hazelcast_some_metric counter
hazelcast_some_metric{instance="your-instance", prefix="your-prefix"} 123
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example Output:
&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%2Fc5pka3vokjhupbg7645d.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%2Fc5pka3vokjhupbg7645d.png" alt=" " width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By following the steps outlined in this blog, you can effectively expose Hazelcast metrics using the JMX Prometheus exporter. This setup will enable you to monitor the performance of your Hazelcast instances, allowing for better management and optimization of your distributed applications. Happy monitoring!&lt;/p&gt;

</description>
      <category>hazelcast</category>
      <category>metrics</category>
      <category>prometheus</category>
      <category>jmx</category>
    </item>
    <item>
      <title>Deploying a Spring Boot Application on AWS: ECS, EKS, or Kubernetes? A Detailed Guide with Cost-Effective Recommendations</title>
      <dc:creator>vishalpaalakurthi</dc:creator>
      <pubDate>Wed, 18 Sep 2024 16:20:22 +0000</pubDate>
      <link>https://dev.to/vishalpaalakurthi/deploying-a-spring-boot-application-on-aws-ecs-eks-or-kubernetes-a-detailed-guide-with-cost-effective-recommendations-1658</link>
      <guid>https://dev.to/vishalpaalakurthi/deploying-a-spring-boot-application-on-aws-ecs-eks-or-kubernetes-a-detailed-guide-with-cost-effective-recommendations-1658</guid>
      <description>&lt;p&gt;Deploying a Spring Boot application as a Docker image in AWS Cloud can be done using several services: &lt;strong&gt;Amazon ECS (Elastic Container Service)&lt;/strong&gt;, &lt;strong&gt;Amazon EKS (Elastic Kubernetes Service)&lt;/strong&gt;, or self-managed &lt;strong&gt;Kubernetes&lt;/strong&gt; on AWS. Here's an overview of the steps for each option, along with their advantages, disadvantages, and cost-effectiveness.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Amazon ECS (Elastic Container Service)&lt;/strong&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Steps to Deploy in ECS:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Create a Docker Image&lt;/strong&gt; of your Spring Boot application and push it to &lt;strong&gt;Amazon ECR (Elastic Container Registry)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Create an &lt;strong&gt;ECS Cluster&lt;/strong&gt; using either &lt;strong&gt;Fargate (serverless)&lt;/strong&gt; or &lt;strong&gt;EC2&lt;/strong&gt; as the underlying infrastructure.&lt;/li&gt;
&lt;li&gt;Define a &lt;strong&gt;Task Definition&lt;/strong&gt; that specifies the Docker image, container ports, and memory/CPU requirements.&lt;/li&gt;
&lt;li&gt;Configure a &lt;strong&gt;Service&lt;/strong&gt; in ECS that links the task definition to the desired number of replicas (auto-scaling) and load balancer (if required).&lt;/li&gt;
&lt;li&gt;Deploy your service and monitor via &lt;strong&gt;CloudWatch&lt;/strong&gt; and ECS dashboard.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Advantages of ECS:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Managed service&lt;/strong&gt;: ECS handles the orchestration, scaling, and health monitoring of containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integration with Fargate&lt;/strong&gt;: You can deploy without managing EC2 instances, reducing operational overhead.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tight AWS integration&lt;/strong&gt;: Easy integration with AWS services like IAM, CloudWatch, ALB, and VPCs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Disadvantages of ECS:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Less flexibility&lt;/strong&gt; compared to Kubernetes for complex workloads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vendor lock-in&lt;/strong&gt; with AWS.&lt;/li&gt;
&lt;li&gt;ECS has limited ecosystem support compared to Kubernetes for third-party tooling.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Cost-effectiveness:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ECS with Fargate&lt;/strong&gt; is cost-effective for smaller workloads or when you don’t want to manage EC2 instances.&lt;/li&gt;
&lt;li&gt;You pay for the vCPU and memory resources your containers use. No cost for managing underlying infrastructure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ECS on EC2&lt;/strong&gt; can become more expensive if not properly optimized, as you're paying for full EC2 instances.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;2. Amazon EKS (Elastic Kubernetes Service)&lt;/strong&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Steps to Deploy in EKS:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Push your Docker image to &lt;strong&gt;ECR&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Create an &lt;strong&gt;EKS Cluster&lt;/strong&gt; with worker nodes on EC2 or &lt;strong&gt;EKS Fargate&lt;/strong&gt; for serverless deployments.&lt;/li&gt;
&lt;li&gt;Configure &lt;strong&gt;kubectl&lt;/strong&gt; or AWS CLI to connect to the EKS cluster.&lt;/li&gt;
&lt;li&gt;Define your &lt;strong&gt;Kubernetes Deployment&lt;/strong&gt; (including replicas, resource requests) and service (LoadBalancer or Ingress).&lt;/li&gt;
&lt;li&gt;Apply the Kubernetes manifest files to deploy your application via &lt;strong&gt;kubectl&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Monitor with &lt;strong&gt;CloudWatch&lt;/strong&gt; or use Kubernetes-native tools like &lt;strong&gt;Prometheus&lt;/strong&gt; and &lt;strong&gt;Grafana&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Advantages of EKS:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Kubernetes-native&lt;/strong&gt;: You can run your Spring Boot application on Kubernetes, which offers the most flexibility and customizability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Huge ecosystem&lt;/strong&gt;: Kubernetes has a large community and ecosystem for monitoring, logging, CI/CD, and autoscaling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-cloud&lt;/strong&gt;: Kubernetes allows easier migration between clouds or even hybrid cloud setups.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Disadvantages of EKS:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Complexity&lt;/strong&gt;: Kubernetes is harder to manage and configure compared to ECS. Even with EKS, you need to manage various Kubernetes components.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Higher operational overhead&lt;/strong&gt;: More expertise is needed to handle Kubernetes, even though AWS manages the control plane.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Cost-effectiveness:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;EKS with &lt;strong&gt;Fargate&lt;/strong&gt; can be &lt;strong&gt;cost-effective for smaller workloads&lt;/strong&gt; as there is no need to manage EC2 instances.&lt;/li&gt;
&lt;li&gt;However, &lt;strong&gt;EKS with EC2&lt;/strong&gt; instances can lead to higher costs due to the need to manage and optimize the nodes.&lt;/li&gt;
&lt;li&gt;You are charged for the &lt;strong&gt;EKS control plane&lt;/strong&gt; (~$74/month per cluster) in addition to EC2 or Fargate resource costs.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;3. Self-managed Kubernetes on AWS (kops or kubeadm)&lt;/strong&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Steps to Deploy on Kubernetes (Self-managed):&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Set up an &lt;strong&gt;EC2 instance&lt;/strong&gt; or multiple EC2 instances for your Kubernetes cluster.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;kops&lt;/strong&gt; (Kubernetes Operations) or &lt;strong&gt;kubeadm&lt;/strong&gt; to create the Kubernetes cluster.&lt;/li&gt;
&lt;li&gt;Push the Docker image to &lt;strong&gt;ECR&lt;/strong&gt; or any other Docker registry.&lt;/li&gt;
&lt;li&gt;Configure &lt;strong&gt;kubectl&lt;/strong&gt; to manage the cluster.&lt;/li&gt;
&lt;li&gt;Define your Kubernetes deployment and service.&lt;/li&gt;
&lt;li&gt;Deploy your application with &lt;strong&gt;kubectl&lt;/strong&gt; and monitor.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Advantages of Self-managed Kubernetes:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Full control&lt;/strong&gt;: You have complete control over the cluster, configuration, and operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No AWS service cost&lt;/strong&gt;: You are not paying for EKS control plane management.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Disadvantages of Self-managed Kubernetes:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;High operational complexity&lt;/strong&gt;: You are responsible for cluster management, security updates, scaling, and monitoring.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limited AWS integration&lt;/strong&gt;: While possible, manual integration is required for AWS services.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scaling difficulty&lt;/strong&gt;: Self-managed clusters need more effort for scaling and disaster recovery.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Cost-effectiveness:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This option can be &lt;strong&gt;cost-efficient for large-scale, long-running applications&lt;/strong&gt; if you have expertise in managing Kubernetes.&lt;/li&gt;
&lt;li&gt;However, it's less cost-effective due to the operational complexity and time required to manage infrastructure compared to using EKS or ECS.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Cost-effective Recommendations:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;For smaller workloads&lt;/strong&gt; or if you want to avoid managing infrastructure, &lt;strong&gt;ECS with Fargate&lt;/strong&gt; is the most cost-effective and easiest option. It provides a serverless experience and scales based on demand without EC2 management.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For medium to large-scale applications&lt;/strong&gt; that need Kubernetes capabilities, &lt;strong&gt;EKS with Fargate&lt;/strong&gt; could be a good balance between cost and flexibility. You avoid the overhead of managing EC2 nodes but still get the power of Kubernetes.&lt;/li&gt;
&lt;li&gt;If you're experienced in Kubernetes and want &lt;strong&gt;full control over your infrastructure&lt;/strong&gt; for a complex or large-scale system, &lt;strong&gt;self-managed Kubernetes&lt;/strong&gt; could be considered, but it will likely require more operational effort.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each option has trade-offs in terms of flexibility, management, and cost, but for most cases, &lt;strong&gt;ECS or EKS with Fargate&lt;/strong&gt; should be the most cost-effective unless your application has specific Kubernetes needs.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>ecs</category>
      <category>eks</category>
      <category>kubernetes</category>
    </item>
    <item>
      <title>Mastering Java Collections with Multithreading: Best Practices and Practical Examples</title>
      <dc:creator>vishalpaalakurthi</dc:creator>
      <pubDate>Wed, 18 Sep 2024 05:59:48 +0000</pubDate>
      <link>https://dev.to/vishalpaalakurthi/mastering-java-collections-with-multithreading-best-practices-and-practical-examples-33c4</link>
      <guid>https://dev.to/vishalpaalakurthi/mastering-java-collections-with-multithreading-best-practices-and-practical-examples-33c4</guid>
      <description>&lt;p&gt;Combining Java Collections with multithreading is a common practice in developing high-performance, concurrent applications. Properly managing collections in a multithreaded environment ensures data integrity, prevents race conditions, and enhances application scalability. This guide provides practical examples demonstrating how to effectively use Java Collections in multithreaded scenarios.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Challenges
&lt;/h2&gt;

&lt;p&gt;When multiple threads access and modify collections concurrently, several issues can arise:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Race Conditions&lt;/strong&gt;: Multiple threads modifying the same data simultaneously can lead to inconsistent or unexpected results.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deadlocks&lt;/strong&gt;: Improper synchronization can cause threads to wait indefinitely for resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance Bottlenecks&lt;/strong&gt;: Excessive synchronization can degrade performance by limiting parallelism.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To address these challenges, Java provides thread-safe collections in the &lt;code&gt;java.util.concurrent&lt;/code&gt; package and synchronization utilities to manage concurrent access effectively.&lt;/p&gt;




&lt;h2&gt;
  
  
  Using Concurrent Collections
&lt;/h2&gt;

&lt;p&gt;Java’s &lt;code&gt;java.util.concurrent&lt;/code&gt; package offers a suite of thread-safe collection classes that handle synchronization internally, providing high performance in concurrent environments.&lt;/p&gt;

&lt;h3&gt;
  
  
  ConcurrentHashMap Example
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ConcurrentHashMap&lt;/code&gt; is a thread-safe implementation of the &lt;code&gt;Map&lt;/code&gt; interface. It allows concurrent read and write operations without locking the entire map, enhancing performance in multithreaded applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case&lt;/strong&gt;: Caching frequently accessed data where multiple threads may read and write concurrently.&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.ConcurrentHashMap&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;ConcurrentHashMapExample&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;ConcurrentHashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;wordCountMap&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;ConcurrentHashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// Method to increment word count&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;incrementWord&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;wordCountMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;merge&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;Integer:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;sum&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;displayCounts&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;wordCountMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;forEach&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;": "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;count&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;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="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;ConcurrentHashMapExample&lt;/span&gt; &lt;span class="n"&gt;example&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;ConcurrentHashMapExample&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"concurrent concurrent multithreading collections java"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Create multiple threads to process words&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;t1&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;split&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;incrementWord&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&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="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;t2&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;split&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;incrementWord&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&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="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;join&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;join&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;displayCounts&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;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ConcurrentHashMap&lt;/code&gt; allows multiple threads to modify the map concurrently.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;merge&lt;/code&gt; method atomically updates the count for each word, preventing race conditions.&lt;/li&gt;
&lt;li&gt;Two threads process the same text concurrently, updating the word counts without interference.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  CopyOnWriteArrayList Example
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;CopyOnWriteArrayList&lt;/code&gt; is a thread-safe variant of &lt;code&gt;ArrayList&lt;/code&gt; where all mutative operations (like &lt;code&gt;add&lt;/code&gt;, &lt;code&gt;set&lt;/code&gt;, and &lt;code&gt;remove&lt;/code&gt;) are implemented by making a fresh copy of the underlying array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case&lt;/strong&gt;: Scenarios with more reads than writes, such as event listener lists.&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.List&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.CopyOnWriteArrayList&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;CopyOnWriteArrayListExample&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;eventListeners&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;CopyOnWriteArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;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;addListener&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;eventListeners&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listener&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;notifyListeners&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;eventListeners&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listener&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" received event: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;event&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;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;CopyOnWriteArrayListExample&lt;/span&gt; &lt;span class="n"&gt;example&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;CopyOnWriteArrayListExample&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Adding listeners in separate threads&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;t1&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addListener&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Listener1"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;t2&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addListener&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Listener2"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

        &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;join&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;join&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Notify all listeners&lt;/span&gt;
        &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;notifyListeners&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"EventA"&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;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;CopyOnWriteArrayList&lt;/code&gt; ensures thread-safe addition of listeners without explicit synchronization.&lt;/li&gt;
&lt;li&gt;Iteration over the list (e.g., notifying listeners) does not require additional synchronization, as the underlying array is safely published.&lt;/li&gt;
&lt;li&gt;Ideal for scenarios where reads are frequent and modifications are rare.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  BlockingQueue Example
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;BlockingQueue&lt;/code&gt; is a thread-safe queue that supports operations that wait for the queue to become non-empty when retrieving and wait for space to become available when storing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case&lt;/strong&gt;: Implementing producer-consumer patterns where producers add tasks to the queue, and consumers process them.&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.ArrayBlockingQueue&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.BlockingQueue&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;BlockingQueueExample&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;BlockingQueue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;taskQueue&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;ArrayBlockingQueue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Producer&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;produce&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;taskQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Produced: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Consumer&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;consume&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;taskQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;take&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Consumed: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;task&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;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;BlockingQueueExample&lt;/span&gt; &lt;span class="n"&gt;example&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;BlockingQueueExample&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Producer thread&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;producer&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&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;tasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Task1"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Task2"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Task3"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Task4"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Task5"&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;produce&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                    &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sleep&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="c1"&gt;// Simulate production time&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;interrupt&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="c1"&gt;// Consumer thread&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;consumer&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;for&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;i&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;consume&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                    &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sleep&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Simulate processing time&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;interrupt&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="n"&gt;producer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;consumer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&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;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ArrayBlockingQueue&lt;/code&gt; is a bounded blocking queue that blocks producers when the queue is full and consumers when it's empty.&lt;/li&gt;
&lt;li&gt;The producer thread adds tasks to the queue, while the consumer thread retrieves and processes them.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;put&lt;/code&gt; and &lt;code&gt;take&lt;/code&gt; methods handle the necessary synchronization, ensuring thread safety without explicit locks.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Synchronizing Access to Collections
&lt;/h2&gt;

&lt;p&gt;Sometimes, you might need to synchronize access to non-concurrent collections to make them thread-safe.&lt;/p&gt;

&lt;h3&gt;
  
  
  Synchronized List Example
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Collections.synchronizedList&lt;/code&gt; provides a synchronized (thread-safe) list backed by the specified list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case&lt;/strong&gt;: When you need a thread-safe version of &lt;code&gt;ArrayList&lt;/code&gt; or &lt;code&gt;LinkedList&lt;/code&gt; without using concurrent variants.&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.ArrayList&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Collections&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.List&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;SynchronizedListExample&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;synchronizedList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;synchronizedList&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;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;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;addItem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;synchronizedList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;displayItems&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;synchronized&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;synchronizedList&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Must synchronize during iteration&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;synchronizedList&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&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="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="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SynchronizedListExample&lt;/span&gt; &lt;span class="n"&gt;example&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;SynchronizedListExample&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Multiple threads adding items&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;t1&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;for&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;i&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addItem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Item "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&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="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;t2&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addItem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Item "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&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="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;join&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;join&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Display items&lt;/span&gt;
        &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;displayItems&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;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Collections.synchronizedList&lt;/code&gt; wraps an &lt;code&gt;ArrayList&lt;/code&gt; to make it thread-safe.&lt;/li&gt;
&lt;li&gt;When iterating over the synchronized list, explicit synchronization is required to prevent &lt;code&gt;ConcurrentModificationException&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This approach is suitable when using existing collection types that don’t have concurrent counterparts.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Producer-Consumer Scenario
&lt;/h2&gt;

&lt;p&gt;Combining concurrent collections and multithreading is ideal for implementing producer-consumer patterns. Here's a comprehensive example using &lt;code&gt;BlockingQueue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case&lt;/strong&gt;: A logging system where multiple threads produce log messages, and a separate thread consumes and writes them to a file.&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.BlockingQueue&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.LinkedBlockingQueue&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Logger class implementing producer-consumer using BlockingQueue&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;Logger&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;BlockingQueue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;logQueue&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;LinkedBlockingQueue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;isRunning&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Producer method to add log messages&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;log&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;logQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Consumer thread to process log messages&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;start&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;consumerThread&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isRunning&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;logQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;logQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;take&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                    &lt;span class="n"&gt;writeToFile&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;interrupt&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="o"&gt;});&lt;/span&gt;
        &lt;span class="n"&gt;consumerThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Simulate writing to a file&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;writeToFile&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Writing to log: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Shutdown the logger&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;stop&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;isRunning&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&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;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="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Logger&lt;/span&gt; &lt;span class="n"&gt;logger&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;Logger&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Simulate multiple threads logging messages&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;t1&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;log&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Message from Thread 1"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;interrupt&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="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;t2&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;log&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Message from Thread 2"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;interrupt&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="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;join&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;join&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Allow some time for consumer to process&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sleep&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stop&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;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;LinkedBlockingQueue&lt;/code&gt; serves as the thread-safe queue between producers (logging threads) and the consumer (logger thread).&lt;/li&gt;
&lt;li&gt;Producers add log messages to the queue using the &lt;code&gt;log&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;The consumer thread continuously takes messages from the queue and processes them (simulated by &lt;code&gt;writeToFile&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;isRunning&lt;/code&gt; flag allows graceful shutdown of the consumer thread after all messages are processed.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prefer Concurrent Collections&lt;/strong&gt;: Whenever possible, use classes from &lt;code&gt;java.util.concurrent&lt;/code&gt; like &lt;code&gt;ConcurrentHashMap&lt;/code&gt;, &lt;code&gt;CopyOnWriteArrayList&lt;/code&gt;, and &lt;code&gt;BlockingQueue&lt;/code&gt; instead of manually synchronizing non-concurrent collections.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minimize Synchronization Scope&lt;/strong&gt;: Keep synchronized blocks as short as possible to reduce contention and improve performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid Using Synchronized Collections for High Contention Scenarios&lt;/strong&gt;: For high-concurrency scenarios, prefer concurrent collections over synchronized wrappers, as they offer better scalability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Handle InterruptedException Properly&lt;/strong&gt;: Always handle &lt;code&gt;InterruptedException&lt;/code&gt; appropriately, typically by restoring the interrupt status with &lt;code&gt;Thread.currentThread().interrupt()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Higher-Level Constructs&lt;/strong&gt;: Utilize higher-level concurrency utilities like &lt;code&gt;ExecutorService&lt;/code&gt;, &lt;code&gt;ForkJoinPool&lt;/code&gt;, and &lt;code&gt;CompletableFuture&lt;/code&gt; to manage threads and tasks more effectively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Immutable Objects&lt;/strong&gt;: Where possible, use immutable objects to simplify concurrency control, as they are inherently thread-safe.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Effectively combining Java Collections with multithreading involves selecting the right collection types and synchronization mechanisms to ensure thread safety and performance. By leveraging concurrent collections provided by the &lt;code&gt;java.util.concurrent&lt;/code&gt; package, developers can build robust and scalable multithreaded applications with ease. Always consider the specific requirements and access patterns of your application to choose the most suitable approach.&lt;/p&gt;

</description>
      <category>java</category>
      <category>multithreading</category>
      <category>synchronization</category>
      <category>collections</category>
    </item>
    <item>
      <title>How to Set Up a PostgreSQL Server on a Virtual Machine</title>
      <dc:creator>vishalpaalakurthi</dc:creator>
      <pubDate>Wed, 29 May 2024 06:57:28 +0000</pubDate>
      <link>https://dev.to/vishalpaalakurthi/how-to-set-up-a-postgresql-server-on-a-virtual-machine-473f</link>
      <guid>https://dev.to/vishalpaalakurthi/how-to-set-up-a-postgresql-server-on-a-virtual-machine-473f</guid>
      <description>&lt;p&gt;In this tutorial, we'll walk through setting up a PostgreSQL server on a Virtual Machine (VM). We'll use Ubuntu as the operating system for the VM and cover steps for popular cloud providers like AWS, Google Cloud, and Azure. Let's get started!&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1: Set Up the VM
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Choose a Cloud Provider&lt;/strong&gt;: AWS, Google Cloud Platform (GCP), or Microsoft Azure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Create a VM&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AWS&lt;/strong&gt;: Use an EC2 instance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GCP&lt;/strong&gt;: Use Compute Engine.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Azure&lt;/strong&gt;: Use Virtual Machine service.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Select OS&lt;/strong&gt;: Choose an Ubuntu LTS version (e.g., Ubuntu 20.04 LTS).&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Step 2: Connect to the VM
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Access the VM&lt;/strong&gt;: Use SSH to connect to the VM.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example:
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; ssh &lt;span class="nt"&gt;-i&lt;/span&gt; your-key.pem username@your-vm-ip-address
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Step 3: Update and Upgrade the System
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Run the following commands to update and upgrade the system:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
   &lt;span class="nb"&gt;sudo &lt;/span&gt;apt upgrade &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 4: Install PostgreSQL
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Install PostgreSQL&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;postgresql postgresql-contrib &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Start and Enable PostgreSQL&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start postgresql
   &lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;postgresql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 5: Configure PostgreSQL
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Switch to the PostgreSQL User&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="nt"&gt;-u&lt;/span&gt; postgres
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Access PostgreSQL Prompt&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   psql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Set a Password for the PostgreSQL User&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="n"&gt;postgres&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Enter the new password when prompted)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Create a New Database and User&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;DATABASE&lt;/span&gt; &lt;span class="n"&gt;mydatabase&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;USER&lt;/span&gt; &lt;span class="n"&gt;myuser&lt;/span&gt; &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="k"&gt;ENCRYPTED&lt;/span&gt; &lt;span class="n"&gt;PASSWORD&lt;/span&gt; &lt;span class="s1"&gt;'mypassword'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt; &lt;span class="k"&gt;PRIVILEGES&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;DATABASE&lt;/span&gt; &lt;span class="n"&gt;mydatabase&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;myuser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Exit PostgreSQL Prompt&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Edit PostgreSQL Configuration to Allow Remote Connections&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open the PostgreSQL configuration file:
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; &lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/postgresql/12/main/postgresql.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Find the line &lt;code&gt;listen_addresses&lt;/code&gt; and set it to &lt;code&gt;'*'&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; listen_addresses = '*'
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Save and close the file.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Configure Client Authentication&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Open the &lt;code&gt;pg_hba.conf&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; &lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/postgresql/12/main/pg_hba.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the following line to allow remote connections:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; host    all             all             0.0.0.0/0            md5
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Save and close the file.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Restart PostgreSQL&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl restart postgresql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 6: Allow External Connections to PostgreSQL
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Update Firewall Rules&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AWS&lt;/strong&gt;: Edit the Security Group to allow inbound traffic on port 5432.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GCP&lt;/strong&gt;: Edit the Firewall rules to allow traffic on port 5432.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Azure&lt;/strong&gt;: Edit the Network Security Group to allow inbound traffic on port 5432.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Step 7: Connect to PostgreSQL Remotely
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Use a PostgreSQL Client&lt;/strong&gt;: Tools like &lt;code&gt;psql&lt;/code&gt;, DBeaver, or pgAdmin can connect to your PostgreSQL server remotely using the VM's public IP address and the credentials you set up.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Example Connection Command
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;psql &lt;span class="nt"&gt;-h&lt;/span&gt; your-vm-ip-address &lt;span class="nt"&gt;-U&lt;/span&gt; myuser &lt;span class="nt"&gt;-d&lt;/span&gt; mydatabase
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Enter the password when prompted)&lt;/p&gt;

&lt;h3&gt;
  
  
  Final Notes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Ensure your VM's firewall settings allow inbound traffic on port 5432.&lt;/li&gt;
&lt;li&gt;Secure your PostgreSQL server by following best practices, such as using strong passwords, enabling SSL, and configuring proper firewall rules.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this setup, you now have a basic PostgreSQL server running on a VM, ready for development or production use. Happy coding!&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>vm</category>
      <category>cloud</category>
    </item>
    <item>
      <title>How to Perform Upsert Operations in MongoDB with Java</title>
      <dc:creator>vishalpaalakurthi</dc:creator>
      <pubDate>Tue, 07 May 2024 07:20:03 +0000</pubDate>
      <link>https://dev.to/vishalpaalakurthi/how-to-perform-upsert-operations-in-mongodb-with-java-3bdf</link>
      <guid>https://dev.to/vishalpaalakurthi/how-to-perform-upsert-operations-in-mongodb-with-java-3bdf</guid>
      <description>&lt;p&gt;When working with MongoDB, one common requirement is to update existing documents with new information or insert new documents if they don't already exist. This is known as an "upsert" operation. In this tutorial, we’ll explore how to perform upsert operations in MongoDB using the Java MongoDB Driver. This is particularly useful for applications where you need to ensure data is always current without risking duplicates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Set Up MongoDB Java Driver
&lt;/h3&gt;

&lt;p&gt;Before you begin coding, you need to include the MongoDB Java Driver in your project. If you are using Maven for project management, you can add the MongoDB Java Driver by including the following dependency in your &lt;code&gt;pom.xml&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.mongodb&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;mongodb-driver-sync&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;4.4.3&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that your project has access to the necessary MongoDB classes and methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Connect to MongoDB
&lt;/h3&gt;

&lt;p&gt;First, establish a connection to your MongoDB instance. This involves creating a client, connecting to the database, and accessing the specific collection you will work with:&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.mongodb.client.MongoClients&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.mongodb.client.MongoClient&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.mongodb.client.MongoDatabase&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.mongodb.client.MongoCollection&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.bson.Document&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;MongoUpsert&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="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MongoClient&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MongoClients&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;create&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"mongodb://localhost:27017"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;MongoDatabase&lt;/span&gt; &lt;span class="n"&gt;database&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getDatabase&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"yourDatabase"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;MongoCollection&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;collection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getCollection&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"yourCollection"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Now ready to perform upsert operations&lt;/span&gt;
            &lt;span class="n"&gt;upsertDocument&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;collection&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="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Define the Upsert Operation
&lt;/h3&gt;

&lt;p&gt;The crux of the upsert functionality lies in how you define the operation. The following method demonstrates setting up an upsert:&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.mongodb.client.model.Filters&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.mongodb.client.model.UpdateOptions&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.mongodb.client.result.UpdateResult&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;upsertDocument&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MongoCollection&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;collection&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;filter&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;Document&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"key"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"value"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Criteria to find the document&lt;/span&gt;
    &lt;span class="nc"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;newData&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;Document&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$set"&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;Document&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"fieldToUpdate"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"newValue"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// Data to update&lt;/span&gt;

    &lt;span class="nc"&gt;UpdateOptions&lt;/span&gt; &lt;span class="n"&gt;options&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;UpdateOptions&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;upsert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Enables upsert functionality&lt;/span&gt;

    &lt;span class="nc"&gt;UpdateResult&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;collection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;updateOne&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newData&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMatchedCount&lt;/span&gt;&lt;span class="o"&gt;()&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;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getUpsertedId&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A new document was inserted with ID: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getUpsertedId&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Existing document updated"&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;h4&gt;
  
  
  Explanation:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Filter&lt;/strong&gt;: Defines how to find the document. If the document with the specified criteria exists, it will be updated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New Data&lt;/strong&gt;: Specifies the new data to be added or updated in the document.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UpdateOptions&lt;/strong&gt;: Setting &lt;code&gt;upsert&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt; tells MongoDB to insert a new document if no existing document matches the filter.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 4: Execute Your Application
&lt;/h3&gt;

&lt;p&gt;Finally, ensure your MongoDB server is running and execute your Java application. This simple setup enables your application to handle inserted or updated documents appropriately, maintaining data integrity and avoiding duplicates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Upsert operations are crucial for maintaining up-to-date data in your database without introducing duplicate records. The MongoDB Java Driver provides a straightforward way to implement these operations, ensuring your data layer remains robust and efficient. Whether you're managing user data, processing transactions, or synchronizing state across applications, mastering upserts is a valuable skill in your MongoDB toolkit.&lt;/p&gt;

</description>
      <category>java</category>
      <category>mongodb</category>
      <category>database</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to create a simple SFTP server using java stack</title>
      <dc:creator>vishalpaalakurthi</dc:creator>
      <pubDate>Thu, 15 Sep 2022 09:23:21 +0000</pubDate>
      <link>https://dev.to/vishalpaalakurthi/build-a-simple-sftp-server-4mh</link>
      <guid>https://dev.to/vishalpaalakurthi/build-a-simple-sftp-server-4mh</guid>
      <description>&lt;p&gt;&lt;strong&gt;Build a simple SFTP server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As a software developer, I do different activities with SFTP server. It became a daily use case for me. I use to ask a IT/DEVOps person for a help to setup SFTP server in an instance, So that i can use through different clients. It is becoming costly activity in terms of time and money and hard to setup in linux/ubuntu machines.&lt;/p&gt;

&lt;p&gt;To avoid that, I have created my own SFTP server and it can run in my local machine too. I used a trending technologies and frameworks and built a light weight SFTP server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tech Stack Used&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JAVA (11)&lt;/li&gt;
&lt;li&gt;Quarkus Framework&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of people may not know about the quarkus. But It's a &lt;em&gt;Kubernetes Native Java stack tailored for OpenJDK HotSpot and GraalVM, crafted from the best of breed Java libraries and standards&lt;/em&gt;. Framework community is growing very fast and building a optimised solutions to the world.&lt;/p&gt;

&lt;p&gt;This is purely build on top of JAVA and QUARKUS. I used a sshd-sftp maven package for implementing SFTP server. So that, Now i can run it locally as jar file using java commands.&lt;/p&gt;

&lt;p&gt;Recently, I used the same jar to create a SFTP server in AWS EC2 instance. It's working cool :).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/vishalpaalakurthi/SFTP-Server-Quarkus" rel="noopener noreferrer"&gt;https://github.com/vishalpaalakurthi/SFTP-Server-Quarkus&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(Instructions are available in README file)&lt;/p&gt;

&lt;p&gt;I hope you like it. Please add your comments.&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;

</description>
      <category>java</category>
      <category>quarkus</category>
      <category>sftp</category>
    </item>
    <item>
      <title>Leveraging Hazelcast Persistence with File System Storage</title>
      <dc:creator>vishalpaalakurthi</dc:creator>
      <pubDate>Fri, 15 Jul 2022 10:52:02 +0000</pubDate>
      <link>https://dev.to/vishalpaalakurthi/how-hazelcast-provides-a-data-persistence-in-community-edition--24ij</link>
      <guid>https://dev.to/vishalpaalakurthi/how-hazelcast-provides-a-data-persistence-in-community-edition--24ij</guid>
      <description>&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%2Fds2u024f7d7l8yrd5yjn.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%2Fds2u024f7d7l8yrd5yjn.png" alt=" " width="800" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hazelcast is a robust, open-source in-memory data grid (IMDG) solution, renowned for its scalability, high performance, and fault tolerance. One of its key features is data persistence, which ensures data durability even in the face of failures. In this article, we'll delve into using Hazelcast persistence with a file system storage backend, exploring its benefits and providing practical examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why File System Storage?
&lt;/h2&gt;

&lt;p&gt;Utilizing a file system for storage in Hazelcast offers several advantages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity&lt;/strong&gt;: File system storage is straightforward to set up and manage, making it an ideal choice for many applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Durability&lt;/strong&gt;: Data stored in files persists even if the application or the Hazelcast cluster is restarted, ensuring high availability and data integrity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: File systems can scale easily by adding more storage capacity, allowing Hazelcast to store vast amounts of data without sacrificing performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compatibility&lt;/strong&gt;: File system storage is compatible with various operating systems and environments, making it versatile for different deployment scenarios.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Setting Up Hazelcast with File System Persistence
&lt;/h2&gt;

&lt;p&gt;To enable Hazelcast persistence with file system storage, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Configure Hazelcast&lt;/strong&gt;: Update your Hazelcast configuration file (&lt;code&gt;hazelcast.xml&lt;/code&gt;) to include the necessary settings for persistence. Specify the persistence type as &lt;code&gt;file&lt;/code&gt;, and provide the directory path where the data will be stored.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;hazelcast&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;persistence&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;base-dir&amp;gt;&lt;/span&gt;/path/to/storage&lt;span class="nt"&gt;&amp;lt;/base-dir&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;enabled&amp;gt;&lt;/span&gt;true&lt;span class="nt"&gt;&amp;lt;/enabled&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;cluster-data&amp;gt;&lt;/span&gt;false&lt;span class="nt"&gt;&amp;lt;/cluster-data&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/persistence&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/hazelcast&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Start Hazelcast&lt;/strong&gt;: Launch your Hazelcast cluster with the updated configuration file. Hazelcast will now persist data to the specified file system directory.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Example Usage
&lt;/h2&gt;

&lt;p&gt;Let's consider a simple Java application that utilizes Hazelcast with file system persistence:&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.hazelcast.core.Hazelcast&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.hazelcast.core.HazelcastInstance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Map&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;HazelcastExample&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="c1"&gt;// Create a Hazelcast instance&lt;/span&gt;
        &lt;span class="nc"&gt;HazelcastInstance&lt;/span&gt; &lt;span class="n"&gt;hazelcastInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Hazelcast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newHazelcastInstance&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Get a distributed map from Hazelcast&lt;/span&gt;
        &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&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;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;distributedMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hazelcastInstance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"my-distributed-map"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Put some data into the map&lt;/span&gt;
        &lt;span class="n"&gt;distributedMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Value 1"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;distributedMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Value 2"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;distributedMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Value 3"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Retrieve data from the map&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Value for key 1: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;distributedMap&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="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Value for key 2: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;distributedMap&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="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Value for key 3: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;distributedMap&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="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

        &lt;span class="c1"&gt;// Shutdown Hazelcast instance&lt;/span&gt;
        &lt;span class="n"&gt;hazelcastInstance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;shutdown&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;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Incorporating file system storage for persistence in Hazelcast offers a reliable solution for storing and managing distributed data. By following the outlined steps and examples, developers can leverage Hazelcast's powerful features while ensuring data durability and fault tolerance. Whether it's for caching, session management, or distributed computing, Hazelcast with file system persistence provides a robust foundation for building scalable and resilient applications.&lt;/p&gt;

&lt;p&gt;Start harnessing the power of Hazelcast persistence with file system storage today and unlock new possibilities for your distributed systems architecture.&lt;/p&gt;

</description>
      <category>hazelcast</category>
      <category>java</category>
      <category>maven</category>
    </item>
    <item>
      <title>Monitoring Cassandra Metrics using Grafana</title>
      <dc:creator>vishalpaalakurthi</dc:creator>
      <pubDate>Wed, 04 Nov 2020 08:36:09 +0000</pubDate>
      <link>https://dev.to/vishalpaalakurthi/monitoring-cassandra-metrics-using-grafana-5bj2</link>
      <guid>https://dev.to/vishalpaalakurthi/monitoring-cassandra-metrics-using-grafana-5bj2</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;Prerequisites&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;jmx_prometheus_javaagent. (&lt;a href="https://mvnrepository.com/artifact/io.prometheus.jmx/jmx_prometheus_javaagent/0.14.0" rel="noopener noreferrer"&gt;https://mvnrepository.com/artifact/io.prometheus.jmx/jmx_prometheus_javaagent/0.14.0&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Sample cassandra.yml configuration for&amp;nbsp;jmx_prometheus_javaagent. (&lt;a href="https://raw.githubusercontent.com/prometheus/jmx_exporter/master/example_configs/cassandra.yml" rel="noopener noreferrer"&gt;https://raw.githubusercontent.com/prometheus/jmx_exporter/master/example_configs/cassandra.yml&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Prometheus server. (&lt;a href="https://prometheus.io/download/" rel="noopener noreferrer"&gt;https://prometheus.io/download/&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Grafana Server. (&lt;a href="https://grafana.com/grafana/download" rel="noopener noreferrer"&gt;https://grafana.com/grafana/download&lt;/a&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Steps to follow&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Download &lt;strong&gt;jmx_prometheus_javaagent&lt;/strong&gt;&amp;nbsp;jar file.&lt;/li&gt;
&lt;li&gt;Download &lt;strong&gt;cassandra.yml&lt;/strong&gt; configuration file for producing jvm and cassandra metrics.&lt;/li&gt;
&lt;li&gt;Add&amp;nbsp;&lt;strong&gt;JVM_OPTS&lt;/strong&gt;&amp;nbsp;to&amp;nbsp;&lt;strong&gt;cassandra-env.sh&lt;/strong&gt;&amp;nbsp;file.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;JVM_OPTS="$JVM_OPTS javaagent:/etc/cassandra/jmx_prometheus_javaagent-0.10.jar=7070:/etc/cassandra/cassandra.yml"&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Restart cassandra server. &lt;a href="http://localhost:7070/metrics" rel="noopener noreferrer"&gt;http://localhost:7070/metrics&lt;/a&gt;, this URL provides JVM and cassandra Metrics.&lt;/li&gt;
&lt;li&gt;Download&amp;nbsp;Prometheus server.&lt;/li&gt;
&lt;li&gt;Add below lines to &lt;strong&gt;prometheus.yml&lt;/strong&gt;&amp;nbsp;for&amp;nbsp;Prometheus server configuration.&lt;strong&gt;&lt;em&gt;job_name: ‘cassandra’static_configs:&lt;/em&gt;&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;targets: [‘ip:7070’]&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Download Grafana Server.&lt;/li&gt;
&lt;li&gt;Setup Prometheus data source&amp;nbsp;to Grafana.&lt;/li&gt;
&lt;li&gt;Create Metrics dashboard using provided queries in Grafana.&lt;/li&gt;
&lt;li&gt;Grafana dashboard is exposed to&amp;nbsp;&lt;em&gt;localhost:3000.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Prometheus server exposed to&amp;nbsp;&lt;em&gt;localhost:9090.&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Notes
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;jmx_prometheus_javaagent&lt;/strong&gt;&amp;nbsp;is to be added to each cassandra node.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Architecture
&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%2Fi%2F89j28ffkgqewqu7m7uvh.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%2Fi%2F89j28ffkgqewqu7m7uvh.png" alt="Alt Text" width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How it works
&lt;/h3&gt;

&lt;p&gt;Prometheus scrapes metrics from instrumented jobs, either directly or via an intermediary push gateway for short-lived jobs. It stores all scraped samples locally and runs rules over this data to either aggregate and record new time series from existing data or generate alerts. Grafana or other API consumers can be used to visualize the collected data.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Steps for setting up Cassandra metrics through grafana.&amp;nbsp;&lt;a href="https://community.grafana.com/t/how-to-connect-prometheus-to-cassandra/1153/11" rel="noopener noreferrer"&gt;(https://community.grafana.com/t/how-to-connect-prometheus-to-cassandra/1153/11&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://grafana.com/docs/grafana/latest/" rel="noopener noreferrer"&gt;https://grafana.com/docs/grafana/latest/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/prometheus/prometheus" rel="noopener noreferrer"&gt;https://github.com/prometheus/prometheus&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://prometheus.io/docs/introduction/overview/" rel="noopener noreferrer"&gt;https://prometheus.io/docs/introduction/overview/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Sample Image
&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%2Fi%2Fwce1ly1gy4z1hlraw6t4.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%2Fi%2Fwce1ly1gy4z1hlraw6t4.png" alt="Alt Text" width="800" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>grafana</category>
      <category>cassandra</category>
      <category>tutorial</category>
      <category>prometheus</category>
    </item>
    <item>
      <title>How to detect loop in linkedlist?</title>
      <dc:creator>vishalpaalakurthi</dc:creator>
      <pubDate>Mon, 26 Oct 2020 14:21:17 +0000</pubDate>
      <link>https://dev.to/vishalpaalakurthi/how-to-detect-loop-in-linkedlist-2k45</link>
      <guid>https://dev.to/vishalpaalakurthi/how-to-detect-loop-in-linkedlist-2k45</guid>
      <description>&lt;p&gt;1) Two pointers (each node is assigned with an address)&lt;/p&gt;

&lt;p&gt;2) Traverse through each node till end , tracking visited node using visited flag.&lt;/p&gt;

&lt;p&gt;If you find node that is already visited, then there is a loop in LinkedList and if you reach till end while traversing then there is no loop in LinkedList&lt;/p&gt;

</description>
      <category>interview</category>
      <category>tutorial</category>
      <category>datastructures</category>
      <category>oops</category>
    </item>
  </channel>
</rss>
