

{"id":18419,"date":"2018-06-14T04:00:38","date_gmt":"2018-06-14T04:00:38","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=18419"},"modified":"2021-12-04T10:15:21","modified_gmt":"2021-12-04T04:45:21","slug":"scala-thread","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/scala-thread\/","title":{"rendered":"What is Scala Thread &amp; Multithreading | File Handling in Scala"},"content":{"rendered":"<p>In our last Scala tutorial, we discussed\u00a0<strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-pattern-matching\/\">Scala Pattern Matching<\/a><\/strong>. Here, we divide this article into two sections- File Handling in Scala Programming Language and Scala Thread. Moreover, how can we write &amp; read files in Scala and multithreading in Scala? Along with this, we will study what is Scala thread and how can we create a thread in Scala. At last, we learn Scala thread methods.<\/p>\n<p>So, let&#8217;s start Scala Thread.<\/p>\n<h3>Scala File Handing<\/h3>\n<p>Scala lets us read from and write to files. Here, we will learn how to open files, read from them, and write to them. Let\u2019s take a quick look.<\/p>\n<h3>How to Write Files in Scala?<\/h3>\n<p>Since Scala doesn\u2019t have a class that will help us with writing files, we use java.io._ from Java.<\/p>\n<p><strong>C:\\Users\\lifei&gt;cd Desktop<\/strong><\/p>\n<p><strong>C:\\Users\\lifei\\Desktop&gt;scala<\/strong><\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-file-io\/\">Refer For More Detail Scala File i\/o: Open, Read and Write a File in Scala<\/a><\/strong><\/p>\n<p>Welcome to Scala 2.12.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_161).<\/p>\n<p>Type in expressions for evaluation. Or try:help.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; import java.io._\r\nimport java.io._\r\nscala&gt; val writer=new PrintWriter(new File(\"demo.txt\"))\r\nwriter: java.io.PrintWriter = java.io.PrintWriter@3c3e363\r\nscala&gt; writer.write(\"Successfully wrote to the file we created\")\r\nscala&gt; writer.close()<\/pre>\n<p>This gives us the following file on the desktop:<\/p>\n<div id=\"attachment_18426\" style=\"width: 746px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/write.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-18426\" class=\"wp-image-18426 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/write.png\" alt=\"Scala Multithreading | File Handling in Scala\" width=\"736\" height=\"190\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/write.png 736w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/write-150x39.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/write-300x77.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/a><p id=\"caption-attachment-18426\" class=\"wp-caption-text\">Scala Multithreading | File Handling in Scala<\/p><\/div>\n<h3>How to Read Files in Scala?<\/h3>\n<p>To read from a file, we use scala.io.Source. Take a look.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; import scala.io.Source\r\nimport scala.io.Source\r\nscala&gt; Source.fromFile(\"demo.txt\").mkString\r\nres2: String = Successfully wrote to the file we created<\/pre>\n<p>But if the file had more than one line:<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; Source.fromFile(\"demo.txt\").getLines.foreach{x=&gt;println(x)}<\/pre>\n<p>Successfully wrote to the file we created<br \/>\nLine 1<br \/>\nLine 2<br \/>\nUsing an <strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-iterator\/\">iterator<\/a><\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; val it=Source.fromFile(\"demo.txt\").getLines()\r\nit: Iterator[String] = non-empty iterator\r\nscala&gt; it.next()\r\nres4: String = Successfully wrote to the file we created\r\nscala&gt; it.next()\r\nres5: String = Line 1\r\nscala&gt; it.next()\r\nres6: String = Line 2<\/pre>\n<p>For more on file handling, read up on Scala File IO.<br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-map\/\">Let&#8217;s Learn Scala Map with Examples Quickly &amp; Effectively<\/a><\/strong><\/p>\n<h3>What is Scala Multithreading?<\/h3>\n<p>Scala supports multithreading, which means we can execute multiple threads at once. We can perform multiple operations independently and achieve multitasking. This lets us develop concurrent applications.<\/p>\n<h3>What is Scala Threads?<\/h3>\n<p>A thread is a lightweight sub-process occupying less memory. To create it, we can either extend the Thread class or the Runnable interface. We can use the run() method then. A thread holds less memory than a full-blown process. Let\u2019s take a look at its life-cycle.<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-closures\/\">Read about Scala Closures with Examples<\/a><\/strong><\/p>\n<h4>a.\u00a0Scala Thread Life-Cycle<\/h4>\n<p>A Scala thread life-cycle is an account of when it starts to when it terminates. It has the following five states:<\/p>\n<p><strong>1. New-<\/strong>\u00a0Scala thread is in this state when it is created. This is effectively the first state.<\/p>\n<p><strong>2. Runnable-<\/strong> When the thread has been created but the thread scheduler hasn\u2019t selected it for running, it is in the runnable state.<\/p>\n<p><strong>3. Running-<\/strong> When the thread scheduler has selected a thread, it is running.<\/p>\n<p><strong>4. Blocked-<\/strong> In the blocked state, the thread is alive, but waiting for input or resources.<\/p>\n<p><strong>5. Terminated-<\/strong> When the run() method exits, the Scala thread is dead.<\/p>\n<div id=\"attachment_18429\" style=\"width: 1003px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/life-cycle.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-18429\" class=\"wp-image-18429 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/life-cycle.png\" alt=\"Scala Multithreading | File Handling in Scala\" width=\"993\" height=\"597\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/life-cycle.png 993w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/life-cycle-150x90.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/life-cycle-300x180.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/life-cycle-768x462.png 768w\" sizes=\"auto, (max-width: 993px) 100vw, 993px\" \/><\/a><p id=\"caption-attachment-18429\" class=\"wp-caption-text\">Scala Multithreading | File Handling in Scala<\/p><\/div>\n<h3><span style=\"font-family: Georgia, Georgia, serif;font-weight: inherit\">How to Create Thread in Scala?<\/span><\/h3>\n<p>Scala Thread is created by two steps:<\/p>\n<div id=\"attachment_18438\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-to-Create-Thread-in-Scala-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-18438\" class=\"wp-image-18438 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-to-Create-Thread-in-Scala-01.jpg\" alt=\"Scala Thread &amp; Scala Multithreading - File handling in Scala\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-to-Create-Thread-in-Scala-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-to-Create-Thread-in-Scala-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-to-Create-Thread-in-Scala-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-to-Create-Thread-in-Scala-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-to-Create-Thread-in-Scala-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-18438\" class=\"wp-caption-text\">How to Create Thread in Scala<\/p><\/div>\n<h4>a. Extending the Thread Class<\/h4>\n<p>Like discussed above, we can extend the Scala thread class or the runnable interface. Let\u2019s try the first option.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; class Demo extends Thread{\r\n| override def run(){\r\n| println(\"Running\")\r\n| }\r\n| }\r\ndefined class Demo\r\nscala&gt; var d=new Demo()\r\nd: Demo = Thread[Thread-2,5,main]\r\nscala&gt; d.start()\r\nscala&gt; Running<\/pre>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-string-interpolation\/\">Let&#8217;s Learn\u00a0<\/a><\/strong><strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-string-interpolation\/\">Scala String Interpolation &#8211; s String, f String\u00a0<\/a><\/strong><\/p>\n<h4>b. Extending the Runnable Interface<\/h4>\n<p>Now, let\u2019s try extending the interface runnable.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; class Demo extends Runnable{\r\n| override def run(){\r\n| println(\"Running\")\r\n| }\r\n| }\r\ndefined class Demo\r\nscala&gt; var d=new Demo()\r\nd: Demo = Demo@7d28cdcd\r\nscala&gt; var t=new Thread(d)\r\nt: Thread = Thread[Thread-2,5,main]\r\nscala&gt; t.start()\r\nscala&gt; Running<\/pre>\n<h3>Scala Thread Methods<\/h3>\n<p>We discussed the states in a Scala thread life-cycle. We can control thread flow to deal with these states using these methods. Let\u2019s take a few examples.<\/p>\n<div id=\"attachment_18435\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Scala-Thread-Methods-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-18435\" class=\"wp-image-18435 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Scala-Thread-Methods-01.jpg\" alt=\"Scala Thread Methods\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Scala-Thread-Methods-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Scala-Thread-Methods-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Scala-Thread-Methods-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Scala-Thread-Methods-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Scala-Thread-Methods-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-18435\" class=\"wp-caption-text\">Scala Thread Methods<\/p><\/div>\n<h4>a. sleep()<\/h4>\n<p>We can use this method to put a Scala thread to sleep for a number of milliseconds. Let\u2019s take an example.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; class Demo extends Thread{\r\n| override def run(){\r\n| for(i&lt;-1 to 7){\r\n| println(i)\r\n| Thread.sleep(400)\r\n| }\r\n| }\r\n| }\r\ndefined class Demo\r\nscala&gt; var d1=new Demo()\r\nd1: Demo = Thread[Thread-3,5,main]\r\nscala&gt; var d2=new Demo()\r\nd2: Demo = Thread[Thread-4,5,main]\r\nscala&gt; d1.start(); d2.start()\r\nscala&gt; 1\r\n1\r\n2\r\n2\r\n3\r\n3\r\n4\r\n4\r\n5\r\n5\r\n6\r\n6\r\n7\r\n7<\/pre>\n<p>This code prints numbers 1 to 7 in pairs. You can say it prints two 1s, then waits for 400 milliseconds, then prints two 2s, then waits 400 milliseconds, and so on.<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-variable\/\">Let&#8217;s read about Scala Variables with Examples<\/a><\/strong><\/p>\n<h4>b. join()<\/h4>\n<p>join() lets us hold execution of the Scala thread currently running until it finishes executing. It waits for a thread to die before it can start more.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; class Demo extends Thread{\r\n| override def run(){\r\n| for(i&lt;-1 to 7){\r\n| println(i)\r\n| Thread.sleep(400)\r\n| }\r\n| }\r\n| }\r\ndefined class Demo\r\nscala&gt; var d1=new Demo(); var d2=new Demo(); var d3=new Demo()\r\nd1: Demo = Thread[Thread-14,5,main]\r\nd2: Demo = Thread[Thread-15,5,main]\r\nd3: Demo = Thread[Thread-16,5,main]\r\nscala&gt; d1.start(); d1.join(); d2.start(); d3.start()\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n1\r\n1\r\nscala&gt; 2\r\n2\r\n3\r\n3\r\n4\r\n4\r\n5\r\n5\r\n6\r\n6\r\n7\r\n7<\/pre>\n<p>In this code, we call start() on d1, then join() on the same object. Then, we call start() on objects d2 and d3. This calls start() on d1; then join() executes it until it dies. And then, it calls start() on d2 and d3, and they run. To understand this, let\u2019s take another example.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; class Demo extends Thread{\r\n| override def run(){\r\n| for(i&lt;-0 to 5){\r\n| println(\"1\")\r\n| Thread.sleep(400)\r\n| }\r\n| }\r\n| }\r\ndefined class Demoscala&gt; class Demo1 extends Thread{\r\n| override def run(){\r\n| println(\"Demo1\")\r\n| Thread.sleep(400)\r\n| }\r\n| }\r\ndefined class Demo1\r\nscala&gt; class Demo2 extends Thread{\r\n| override def run(){\r\n| println(\"Demo2\")\r\n| Thread.sleep(400)\r\n| }\r\n| }\r\ndefined class Demo2\r\nscala&gt; var d=new Demo(); var d1=new Demo1(); var d2=new Demo2()\r\nd: Demo = Thread[Thread-29,5,main]\r\nd1: Demo1 = Thread[Thread-30,5,main]\r\nd2: Demo2 = Thread[Thread-31,5,main]\r\nscala&gt; d.start(); d.join(); d1.start(); d2.start()\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\nDemo1\r\nDemo2<\/pre>\n<p>In this example, we see in the REPL that 1 prints six times with an interval of 400 milliseconds. And then, Demo1 and Demo2 print simultaneously. This clears it. Let\u2019s move to another method.<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-else-statements-statements\/\">Let&#8217;s See Scala if-else Statements with Statements<\/a><\/strong><\/p>\n<h4>c. setName() and getName()<\/h4>\n<p>The methods setName() and getName() let us set and get names of threads. Let\u2019s see how.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; class Demo extends Thread{\r\n| override def run(){\r\n| for(i&lt;-1 to 7){\r\n| println(this.getName()+\"- \"+i)\r\n| Thread.sleep(400)\r\n| }\r\n| }\r\n| }\r\ndefined class Demo\r\nscala&gt; var d1=new Demo(); var d2=new Demo(); var d3=new Demo()\r\nd1: Demo = Thread[Thread-35,5,main]\r\nd2: Demo = Thread[Thread-36,5,main]\r\nd3: Demo = Thread[Thread-37,5,main]\r\nscala&gt; d1.setName(\"One\"); d2.setName(\"Two\"); d3.setName(\"Three\")\r\nscala&gt; d1.start(); d2.start(); d3.start()\r\nTwo- 1\r\nOne- 1\r\nThree- 1\r\nscala&gt; Two- 2\r\nOne- 2\r\nThree- 2\r\nTwo- 3\r\nOne- 3\r\nThree- 3\r\nTwo- 4\r\nOne- 4\r\nThree- 4\r\nTwo- 5\r\nOne- 5\r\nThree- 5\r\nTwo- 6\r\nOne- 6\r\nThree- 6\r\nTwo- 7\r\nOne- 7\r\nThree- 7<\/pre>\n<h4>This code runs the loop seven times for each Scala thread. This time, the order is Two, One, Three. The next time you try it, it may be One, Three, Two.<\/h4>\n<h4>d. getPriority() and setPriority()<\/h4>\n<p>Using these methods, we can get and set the priority for a\u00a0Scala thread. Let\u2019s take an example.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; class Demo extends Thread{\r\n| override def run(){\r\n| for(i&lt;-1 to 7){\r\n| println(this.getName())\r\n| println(this.getPriority())\r\n| Thread.sleep(400)\r\n| }\r\n| }\r\n| }\r\ndefined class Demo\r\nscala&gt; var d1=new Demo(); var d2=new Demo()\r\nd1: Demo = Thread[Thread-47,5,main]\r\nd2: Demo = Thread[Thread-48,5,main]\r\nscala&gt; d1.setName(\"One\"); d2.setName(\"Two\")\r\nscala&gt; d1.setPriority(Thread.MIN_PRIORITY); d2.setPriority(Thread.MAX_PRIORITY)\r\nscala&gt; d1.start(); d2.start()\r\nscala&gt; Two\r\nOne\r\n10\r\n1\r\nTwo\r\n10\r\nOne\r\n1\r\nTwo\r\n10\r\nOne\r\n1\r\nTwo\r\n10\r\nOne\r\n1\r\nTwo\r\n10\r\nOne\r\n1\r\nTwo\r\n10\r\nOne\r\n1\r\nTwo\r\n10\r\nOne\r\n1<\/pre>\n<p>This example tells us that the maximum priority is 10 and the minimum is 1.<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-access-modifiers\/\">Have a look &#8211; Scala Access Modifiers: Public, Private and Protected Members<\/a><\/strong><\/p>\n<h4>e. task()<\/h4>\n<p>In this example, we create multiple Scala threads and use them to run multiple tasks. This lets us implement multitasking in Scala.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; class Demo extends Thread{\r\n| override def run(){\r\n| for(i&lt;-1 to 4){\r\n| println(i)\r\n| Thread.sleep(400)\r\n| }\r\n| }\r\n| def task(){\r\n| for(i&lt;-5 to 8){\r\n| println(i)\r\n| Thread.sleep(200)\r\n| }\r\n| }\r\n| }\r\ndefined class Demo\r\nscala&gt; var d1=new Demo()\r\nd1: Demo = Thread[Thread-51,5,main]\r\nscala&gt; d1.start(); d1.task()\r\n1\r\n5\r\n6\r\n2\r\n7\r\n8\r\n3\r\nscala&gt; 4<\/pre>\n<h4>f. More Methods<\/h4>\n<p>Other than and including what we just saw, we have the following methods:<\/p>\n<p><strong>1. Public static void sleep(long millis) throws InterruptedException-<\/strong> This puts the thread to sleep for a certain number of milliseconds.<\/p>\n<p><strong>2. Public final String getName()-<\/strong> This method gets the name of the thread.<\/p>\n<p><strong>3. Public final void setName(String name)-<\/strong> This method lets us set the name of the thread.<\/p>\n<p><strong>4. Public final int getPriority()-<\/strong> This method gives us the priority of the thread.<\/p>\n<p><strong>5. Public final void setPriority(int newPriority)-<\/strong> This method lets us set priority for a thread.<\/p>\n<p><strong>6. Public final boolean isAlive()-<\/strong> This tells us if a thread is alive (somewhere between states New and Terminated.<\/p>\n<p><strong>7. Public final void join() throws InterruptedException-<\/strong> Like we\u2019ve seen, this method waits for the thread to die before starting another.<\/p>\n<p><strong>8. Public static void yield()-<\/strong> Yield compels the current running thread to give up control to other threads. It pauses the current thread.<\/p>\n<p><strong>9. Public Thread.State getState()-<\/strong> This returns the state of the thread. It lets us monitor system state.<\/p>\n<p>So, this was all about Scala Thread &amp; Multithreading. Hope you like our explanation.<\/p>\n<h3>Conclusion<\/h3>\n<p>Hence, in this Scala thread tutorial, we studied what is threading in Scala Programming. Along with this, we learned about File handling in Scala. Furthermore, if you have any query regarding Scala Threading, Feel free to ask in the comment section.<br \/>\nRelated Topic-\u00a0<strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-trait-mixins\/\">\u00a0What is Scala Trait Mixins <\/a><\/strong><br \/>\n<strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Scala_(programming_language)\">For reference<\/a><\/strong><span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1920,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Scala_(programming_language)&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250919075050\\\/https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Scala_(programming_language)&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-10 09:49:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-14 06:04:41&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-17 08:14:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-21 15:16:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-24 20:50:35&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-27 22:51:06&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-02 00:56:48&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-05 05:12:52&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-08 16:37:57&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-11 16:52:40&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-15 01:28:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-19 03:57:54&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-23 15:04:21&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-27 23:53:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-31 15:57:47&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-03 17:23:23&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-07 12:58:30&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-10 13:51:06&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-13 14:05:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-17 10:42:41&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-20 19:37:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-24 11:37:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-28 03:48:25&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-03-03 20:07:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-09 03:36:50&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-12 17:53:12&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-16 12:24:01&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-03-21 16:15:51&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-29 09:06:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-02 02:19:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-05 07:25:42&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-09 05:45:52&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-13 17:35:26&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-17 20:46:47&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-20 23:10:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-24 16:10:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-29 02:08:36&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-03 06:35:12&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-07 05:36:56&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-10 12:10:35&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-13 17:35:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-18 05:54:28&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-22 04:26:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-25 10:55:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-29 13:13:25&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-01 14:38:15&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-05 08:23:46&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-09 21:06:00&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-13 16:35:11&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-18 05:49:58&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-22 04:17:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-26 15:19:02&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-29 17:32:11&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-07-03 01:11:06&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-07-06 09:48:22&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-07-06 09:48:22&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last Scala tutorial, we discussed\u00a0Scala Pattern Matching. Here, we divide this article into two sections- File Handling in Scala Programming Language and Scala Thread. Moreover, how can we write &amp; read files&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":18428,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[16491,4682,8966,11376,12448,12507,12560,14712,15945,16298],"class_list":["post-18419","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scala","tag-create-thread-in-scala","tag-file-handling-in-scala","tag-multithreading-in-scala","tag-reading-files-in-scala","tag-scala-file-handling","tag-scala-multithreading-tutorial","tag-scala-thread-methods","tag-thread-in-scala","tag-what-is-scala-threads","tag-writing-files-in-scala"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What is Scala Thread &amp; Multithreading | File Handling in Scala - DataFlair<\/title>\n<meta name=\"description\" content=\"Scala Thread Tutorial,File handling in Scala,Scala Multithreading,Scala Thread Life-Cycle, Create Threading, Read\/Write Files in Scala,Scala Thread methods\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/scala-thread\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Scala Thread &amp; Multithreading | File Handling in Scala - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Scala Thread Tutorial,File handling in Scala,Scala Multithreading,Scala Thread Life-Cycle, Create Threading, Read\/Write Files in Scala,Scala Thread methods\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/scala-thread\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-06-14T04:00:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-04T04:45:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/File-Handling-and-Multithreading-in-Scala-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is Scala Thread &amp; Multithreading | File Handling in Scala - DataFlair","description":"Scala Thread Tutorial,File handling in Scala,Scala Multithreading,Scala Thread Life-Cycle, Create Threading, Read\/Write Files in Scala,Scala Thread methods","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/scala-thread\/","og_locale":"en_US","og_type":"article","og_title":"What is Scala Thread &amp; Multithreading | File Handling in Scala - DataFlair","og_description":"Scala Thread Tutorial,File handling in Scala,Scala Multithreading,Scala Thread Life-Cycle, Create Threading, Read\/Write Files in Scala,Scala Thread methods","og_url":"https:\/\/data-flair.training\/blogs\/scala-thread\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-06-14T04:00:38+00:00","article_modified_time":"2021-12-04T04:45:21+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/File-Handling-and-Multithreading-in-Scala-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/scala-thread\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/scala-thread\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"What is Scala Thread &amp; Multithreading | File Handling in Scala","datePublished":"2018-06-14T04:00:38+00:00","dateModified":"2021-12-04T04:45:21+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scala-thread\/"},"wordCount":1142,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scala-thread\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/File-Handling-and-Multithreading-in-Scala-01.jpg","keywords":["Create Thread in Scala","File Handling in Scala","Multithreading in Scala","reading Files in Scala","Scala File Handling","Scala Multithreading Tutorial","Scala Thread Methods","Thread in Scala","What is Scala Threads","Writing Files in Scala"],"articleSection":["Scala Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/scala-thread\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/scala-thread\/","url":"https:\/\/data-flair.training\/blogs\/scala-thread\/","name":"What is Scala Thread &amp; Multithreading | File Handling in Scala - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scala-thread\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scala-thread\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/File-Handling-and-Multithreading-in-Scala-01.jpg","datePublished":"2018-06-14T04:00:38+00:00","dateModified":"2021-12-04T04:45:21+00:00","description":"Scala Thread Tutorial,File handling in Scala,Scala Multithreading,Scala Thread Life-Cycle, Create Threading, Read\/Write Files in Scala,Scala Thread methods","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/scala-thread\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/scala-thread\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/scala-thread\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/File-Handling-and-Multithreading-in-Scala-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/File-Handling-and-Multithreading-in-Scala-01.jpg","width":1200,"height":628,"caption":"Scala Thread &amp; Multithreading | File Handling in Scala"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/scala-thread\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Scala Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/scala\/"},{"@type":"ListItem","position":3,"name":"What is Scala Thread &amp; Multithreading | File Handling in Scala"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/18419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=18419"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/18419\/revisions"}],"predecessor-version":[{"id":104731,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/18419\/revisions\/104731"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/18428"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=18419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=18419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=18419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}