Browse Source

Created some basic framework classes to use for my testing of multi threaded file io.

pull/18/head
jrtechs 5 years ago
parent
commit
72e484e046
3 changed files with 140 additions and 0 deletions
  1. +44
    -0
      multiThreadedFileIO/FileReader.java
  2. +20
    -0
      multiThreadedFileIO/ReadTask.java
  3. +76
    -0
      multiThreadedFileIO/TaskManager.java

+ 44
- 0
multiThreadedFileIO/FileReader.java View File

@ -0,0 +1,44 @@
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Simple class for reading files as strings.
*
* @author Jeffery Russell
*/
public class FileReader
{
public static String readFile(String filePath)
{
String fileContent = new String();
try
{
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(filePath)));
String line;
while ((line = br.readLine()) != null)
{
fileContent = fileContent.concat(line);
}
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return fileContent;
}
public static void main(String[] args)
{
System.out.println("Test");
System.out.println(FileReader.readFile("./testData/data1.txt"));
}
}

+ 20
- 0
multiThreadedFileIO/ReadTask.java View File

@ -0,0 +1,20 @@
/**
* Simple method to be used by the task manager to do
* file io.
*
* @author Jeffery Russell
*/
public class ReadTask
{
private String fileName;
public ReadTask(String fileName)
{
this.fileName = fileName;
}
public void runTask()
{
FileReader.readFile(fileName);
}
}

+ 76
- 0
multiThreadedFileIO/TaskManager.java View File

@ -0,0 +1,76 @@
import java.util.List;
import java.util.Vector;
/**
* A class which enables user to run a large chunk of
* tasks in parallel efficiently.
*
* @author Jeffery 1-29-19
*/
public class TaskManager
{
/** Number of threads to use at once */
private int threadCount;
/** Meaningless tasks to run in parallel */
private List<ReadTask> tasks;
public TaskManager(int threadCount)
{
this.threadCount = threadCount;
//using vectors because they are thread safe
this.tasks = new Vector<>();
}
public void addTask(ReadTask t)
{
tasks.add(t);
}
/**
* This is the fun method.
*
* This will run all of the tasks in parallel using the
* desired amount of threads untill all of the jobs are
* complete.
*/
public void runTasks()
{
int desiredThreads = threadCount > tasks.size() ?
tasks.size() : threadCount;
Thread[] runners = new Thread[desiredThreads];
System.out.println("Total Tasks: " + tasks.size());
System.out.println("Threads Used:" + desiredThreads);
for(int i = 0; i < desiredThreads; i++)
{
runners[i] = new Thread(()->
{
while(!tasks.isEmpty())
{
ReadTask t = tasks.remove(0);
t.runTask();
}
});
runners[i].start();
}
for(int i = 0; i < desiredThreads; i++)
{
try
{
runners[i].join();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}

Loading…
Cancel
Save