I am a beginner in Java.Please help me solve my problem. I want to perform hashed based search for patterns present in files.The patterns are provided at run time. The search is to be performed on multiple files and each file is almost 1 MB.If I execute the search function serially on all files, its taking a lot of time. I wanted to perform the search operation in parallel on all such 12 files. Please guide me how to reduce the time of execution.
Asked
Active
Viewed 623 times
0
-
Your machine probably has space for 1,000 MB if not 10,000 MB. At home I have one with over 100,000 MB of memory. 1 MB is small enough that you don't need to worry about size. – Peter Lawrey Feb 12 '17 at 15:05
-
@PeterLawrey You have a machine with 100GB of RAM? – weston Feb 12 '17 at 15:08
-
1@weston My 9 yo has an old PC of mine which is dual screen and has 24 GB of memory. I think it's time to upgrade to 256 GB. I did this test at home ;) https://vanilla-java.github.io/2017/01/27/Chronicle-Queue-storing-1-TB-in-virtual-memory-on-a-128-GB-machine.html You might be interested to see what a JVM which is over 1 TB of virtual memory looks like on `top` ;) – Peter Lawrey Feb 12 '17 at 15:11
-
@PeterLawrey lol, my laptop and desktop have less combined (22GB) than your 9 yo's! But the same number of screens at least! Interesting read, thanks. – weston Feb 12 '17 at 15:16
1 Answers
0
You can use Threads
. They run parallel to each other.
Here's an example:
Thread thread = new Thread() { public void run(){ //Here you put your code } }; thread.start();
OR, if you're using Java 8:
Thread thread = new Thread(() -> { //Here you put your code }); thread.start();
OR make a
Thread
subclass and overriderun()
method:public class MyThread extends Thread { public void run() { //Here you put your code } } //Then, in another class: MyThread mt = new MyThread(); mt.start();
OR make a class implementing
Runnable
and overriderun()
method:public class MyRunnable implements Runnable { public void run() { //Here you put your code } } //Then, in another class: Thread th = new Thread(new MyRunnable()); th.start();
Also you can follow official documentation on Threads
.

u32i64
- 2,384
- 3
- 22
- 36