<?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: Thai Dao</title>
    <description>The latest articles on DEV Community by Thai Dao (@thaidao6314).</description>
    <link>https://dev.to/thaidao6314</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3984038%2F8fbc9f58-4d12-4973-8b2c-f8084f1d6471.jpeg</url>
      <title>DEV Community: Thai Dao</title>
      <link>https://dev.to/thaidao6314</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thaidao6314"/>
    <language>en</language>
    <item>
      <title>Stop Blocking Your Threads: Thread.sleep() vs. Task.sleep() in Swift</title>
      <dc:creator>Thai Dao</dc:creator>
      <pubDate>Sun, 14 Jun 2026 15:06:53 +0000</pubDate>
      <link>https://dev.to/thaidao6314/stop-blocking-your-threads-threadsleep-vs-tasksleep-in-swift-oh6</link>
      <guid>https://dev.to/thaidao6314/stop-blocking-your-threads-threadsleep-vs-tasksleep-in-swift-oh6</guid>
      <description>&lt;p&gt;When I first started exploring &lt;strong&gt;Swift Concurrency&lt;/strong&gt;, a simple question popped into&lt;br&gt;
my mind: "&lt;em&gt;We’ve had &lt;code&gt;Thread.sleep()&lt;/code&gt; for years and it works fine. Why did Apple introduce &lt;code&gt;Task.sleep()&lt;/code&gt;? Aren't they doing the exact same thing?&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;Well, after a few "frozen UI" incidents and some deep dives into the docs, I realized that while they both "pause" execution, their impact on the system is worlds apart.&lt;/p&gt;

&lt;p&gt;Let’s break it down.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Thread.sleep(): The "Selfish" Way&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Thread.sleep(forTimeInterval:)&lt;/code&gt; is a synchronous operation. When you call this, you are telling the Operating System: "&lt;em&gt;Stop this entire thread right now. Don't let it do anything else for the next X seconds.&lt;/em&gt;"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This blocks the entire thread&lt;/span&gt;
&lt;span class="kt"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;forTimeInterval&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I'm awake!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Problem:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Blocking&lt;/strong&gt;: If you call this on the &lt;strong&gt;Main Thread&lt;/strong&gt;, your UI freezes. No scrolling, no button clicks—nothing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource Heavy&lt;/strong&gt;: The OS still has to maintain that thread's resources, even though it's sitting idle doing zero work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inefficient&lt;/strong&gt;: In a world of limited threads, "holding" a thread hostage while it sleeps is a waste of power.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Task.sleep(): The "Polite" Way&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Task.sleep()&lt;/code&gt; is built for the modern &lt;code&gt;async/await&lt;/code&gt; world. It doesn't block the thread; it &lt;strong&gt;suspends&lt;/strong&gt; the task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This suspends the task, not the thread&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kt"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Task resumed!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why it’s better:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Non-blocking&lt;/strong&gt;: When a task hits &lt;code&gt;await Task.sleep()&lt;/code&gt;, it gives up its seat on the thread.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thread Reusability&lt;/strong&gt;: The Swift Concurrency runtime is smart. While your task is "sleeping," it uses that same thread to run other tasks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System-Friendly&lt;/strong&gt;: Once the timer is up, the system finds an available thread to resume your task.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: &lt;code&gt;Thread.sleep&lt;/code&gt; is like a person standing in the middle of a doorway to take a nap. No one can pass. &lt;code&gt;Task.sleep&lt;/code&gt; is like a person stepping out of the room to nap so others can still use the door.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;A Quick Note on "Blocking" and Deadlocks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Understanding the difference between &lt;strong&gt;blocking&lt;/strong&gt; and &lt;strong&gt;suspending&lt;/strong&gt; is key to avoiding the dreaded Deadlock.&lt;/p&gt;

&lt;p&gt;A classic mistake many of us have made is calling &lt;code&gt;.sync&lt;/code&gt; on the Main Thread from the Main Thread:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// DO NOT DO THIS on the Main Thread&lt;/span&gt;
&lt;span class="kt"&gt;DispatchQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sync&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This will never print"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why does this crash/hang?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Main Thread says&lt;/strong&gt;: "&lt;em&gt;I will wait until this block of code finishes before I move to the next line.&lt;/em&gt;" (That's what .&lt;code&gt;sync&lt;/code&gt; does).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The Block of Code&lt;/strong&gt; is sent to the Main Queue, waiting for the Main Thread to become free so it can run.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The Result&lt;/strong&gt;: The Thread is waiting for the Block, and the Block is waiting for the Thread. They are stuck in a "forever-waiting" loop. &lt;strong&gt;Deadlock&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The Verdict&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Thread.sleep() = Blocking&lt;/strong&gt;. Old school. Use only if you have a very specific reason to stop a background thread entirely.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Task.sleep() = Suspending&lt;/strong&gt;. Modern. Use this in 99% of your &lt;code&gt;async/await&lt;/code&gt; code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Rule of thumb&lt;/strong&gt;: In Swift Concurrency, think in &lt;strong&gt;Tasks&lt;/strong&gt;, not Threads. Don't block the pool; let the runtime manage the resources for you.&lt;/p&gt;

&lt;p&gt;Are you still using &lt;code&gt;Thread.sleep()&lt;/code&gt; in your projects, or have you fully migrated to &lt;code&gt;async/await&lt;/code&gt;? Let’s chat in the comments!&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>watercooler</category>
    </item>
  </channel>
</rss>
