Question: I need RunableClassA file. Threads Thread #1 - IntegerSorter class - sorts 100k random integers (values ranging from 0-100,000) Thread #2 - StringSorter class -

I need RunableClassA file.

Threads

Thread #1 - IntegerSorter class - sorts 100k random integers (values ranging from 0-100,000)

Thread #2 - StringSorter class - sorts 30k element String array (read 30k words from provided dictionary.txt file)

Driver.java, which includes main and invokes both threads (example given in this presentation)

3 classes

RunnableClassA.java

This class will sort integers

constructor to initialize member variables

run() method

calls generate()

calls sort()

calls write()

start() method

RunnableClassB.java

This class will sort Strings

constructor to initialize member variables

run() method

calls read()

calls sort()

calls write()

start() method

Driver.java

Creates RunnableClassA object and calls the start()

Creates RunnableClassB object and calls the start()

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

public class RunableClassA implements Runnable{

public void run(){

generate();

sort();

write();

}

public void generate(){

}

public void sort(){

}

public void write(){

}

public void start(){

if(t == null);

t = new Thread(this, "My Thread Name");

t.start();

}

}

public class RunableClassB implements Runnable{

Thread t;

public void run(){

read();

sort();

write();

}

public void read(){

BufferedReader reader = null;

BufferedWriter writer = null;

//Create an ArrayList object to hold the lines of input file

ArrayList lines = new ArrayList();

try{

//Creating BufferedReader object to read the input file

reader = new BufferedReader(new FileReader("dictionary.txt"));

//Reading all the lines of input file one by one and adding them into ArrayList

String currentLine = reader.readLine();

System.out.println("Output after Read Operation");

while (currentLine != null){

lines.add(currentLine);

System.out.println(currentLine);

currentLine = reader.readLine();

}

}

catch (IOException e){

e.printStackTrace();

}

}

public void sort(){

BufferedReader reader = null;

BufferedWriter writer = null;

//Create an ArrayList object to hold the lines of input file

ArrayList lines = new ArrayList();

try{

//Creating BufferedReader object to read the input file

reader = new BufferedReader(new FileReader("dictionary.txt"));

//Reading all the lines of input file one by one and adding them into ArrayList

String currentLine = reader.readLine();

while (currentLine != null){

lines.add(currentLine);

currentLine = reader.readLine();

}

Collections.sort(lines);

writer = new BufferedWriter(new FileWriter("output.txt"));

System.out.println("Print after Sort");

for (String line : lines){

System.out.println(line);

}

}

catch (IOException e){

e.printStackTrace();

}

finally{

//Closing the resources

try{

if (reader != null){

reader.close();

}

if(writer != null){

writer.close();

}

}

catch (IOException e){

e.printStackTrace();

}

}

}

public void write(){

BufferedReader reader = null;

BufferedWriter writer = null;

//Create an ArrayList object to hold the lines of input file

ArrayList lines = new ArrayList();

try{

//Creating BufferedReader object to read the input file

reader = new BufferedReader(new FileReader("dictionary.txt"));

//Reading all the lines of input file one by one and adding them into ArrayList

String currentLine = reader.readLine();

while (currentLine != null){

lines.add(currentLine);

currentLine = reader.readLine();

}

Collections.sort(lines);

writer = new BufferedWriter(new FileWriter("output.txt"));

for (String line : lines){

writer.write(line);

writer.newLine();

}

}

catch (IOException e){

e.printStackTrace();

}

finally{

//Closing the resources

try{

if (reader != null){

reader.close();

}

if(writer != null){

writer.close();

}

}

catch (IOException e){

e.printStackTrace();

}

}

}

public void start(){

if(t == null);

t = new Thread(this, "My Thread Name");

t.start();

}

}

public class Driver{

public static void main(String args[]){

RunableClassA r = new RunableClassA();

r.start();

RunableClassB r2 = new RunableClassB();

r2.start();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!