Reading a File extending Thread API
- ReadFile.java has a run() method which implements the reading the file code within the try with resources block
- In the main method start method is called over the ReadFile class instance
- In thread we have coded is asynchrobnous(order of execution cannot be guaranteed) which we can see from the output below
TestThread.java
package com.mugil.test; import com.mugil.runnables.ReadFile; public class TestThread { public static void main(String[] args) { ReadFile objReadFileThread1 = new ReadFile(); ReadFile objReadFileThread2 = new ReadFile(); ReadFile objReadFileThread3 = new ReadFile(); objReadFileThread1.start(); objReadFileThread2.start(); objReadFileThread3.start(); } }
ReadFile.java
package com.mugil.runnables; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class ReadFile extends Thread { public void run() { try (BufferedReader reader = new BufferedReader(new FileReader(new File("E:\\JavaProjects\\JavaThreads\\src\\Sample.txt")))) { String line = null; while ((line = reader.readLine()) != null) { System.out.println(Thread.currentThread().getName() + " reading line " + line); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Output
Thread-2 reading line Line1 Thread-0 reading line Line1 Thread-0 reading line Line2 Thread-0 reading line Line3 Thread-1 reading line Line1 Thread-1 reading line Line2 Thread-2 reading line Line2 Thread-1 reading line Line3 Thread-1 reading line Line4 Thread-1 reading line Line5 Thread-0 reading line Line4 Thread-0 reading line Line5 Thread-2 reading line Line3 Thread-2 reading line Line4 Thread-2 reading line Line5
Reading a File implementing Runnable API
- Now in the below code the runnable API is implemented rather than extending like Thread
- The run() is called over instance of ReadFile rather than start() method
- Calling run() method will start the execution of thread in the present running thread rather than creating new Thread for execution which can been seen in output main reading line rather than Thread-N reading line
TestThread.java
package com.mugil.test; import com.mugil.runnables.ReadFile; public class TestThread { public static void main(String[] args) { ReadFile objReadFileThread1 = new ReadFile(); ReadFile objReadFileThread2 = new ReadFile(); ReadFile objReadFileThread3 = new ReadFile(); objReadFileThread1.run(); objReadFileThread2.run(); objReadFileThread3.run(); } }
ReadFile.java
public class ReadFile implements Runnable { public void run() { try (BufferedReader reader = new BufferedReader(new FileReader(new File("E:\\JavaProjects\\JavaThreads\\src\\Sample.txt")))) { String line = null; while ((line = reader.readLine()) != null) { System.out.println(Thread.currentThread().getName() + " reading line " + line); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Output
main reading line Line1 main reading line Line2 main reading line Line3 main reading line Line4 main reading line Line5 main reading line Line1 main reading line Line2 main reading line Line3 main reading line Line4 main reading line Line5 main reading line Line1 main reading line Line2 main reading line Line3 main reading line Line4 main reading line Line5
Methods to Manage thread are available on Thread class not in Runnable. So we can pass the runnable instance as parameter like one below
TestThread.java
. . . Thread objThread = new Thread(runObj); objThread.start(); . .