Created
October 15, 2019 08:04
-
-
Save ayu-24/67c67730daa71b1e0ec4301e3f0f4813 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
public class Count implements Runnable { | |
private static long processedTime = 0; | |
File file; | |
public Count(File fileName) { | |
file = fileName; | |
} | |
public static void main(String[] args) throws InterruptedException { | |
File file = new File("/home/workspace/enwik8"); | |
Runnable runnable = new Count(file); | |
long startTime = System.currentTimeMillis(); | |
Thread thread; | |
for (int i = 0; i < 50; i++) { | |
thread =new Thread(runnable); | |
thread.start(); | |
thread.join(); | |
} | |
long elapsedTime = System.currentTimeMillis() - startTime; | |
System.out.println("Threads processing time " + elapsedTime + "milliseconds"); | |
} | |
public void run() { | |
int i = 0; | |
int wordCount = 0; | |
try { | |
FileReader reader = new FileReader(file); | |
BufferedReader buffer = new BufferedReader(reader); | |
String line; | |
while ((line = buffer.readLine()) != null) { | |
String[] wordList = line.split(","); | |
wordCount += wordList.length; | |
} | |
System.out.println("Number of words in " + Thread.currentThread().getName() + "-" + wordCount); | |
reader.close(); | |
} | |
catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@RostMyr is correct. This code is not executing tasks in parallel.