Results 1 to 2 of 2
Thread: using Thread
- 11-20-2007, 04:33 AM #1
Member
- Join Date
- Aug 2007
- Posts
- 15
- Rep Power
- 0
Thread
Hi Guys
Nowdays I am developing programe to search particulr file in the folder.In here what i am doing is getting files list in the given foder and save it in to string array.Ater that search process start from the first element of the string array and in the same time another search process start form the final elemnt to first element ,it'e mean backward searching.I think doing like above procedure it's will take less time compare to noraml searching.
so,what i want to do is using thread.Please I kindly accept any suggestion for do this.
Thank youLast edited by one198; 11-20-2007 at 05:14 AM.
- 11-21-2007, 08:01 AM #2
Java Code:import java.io.*; import java.util.*; public class SearchTest { static Random seed = new Random(); static String[] fileNames; static String target; static long start; public static void main(String[] args) { String searchDir = "."; File folder = new File(searchDir); List<String> list = new ArrayList<String>(); collectFileNames(folder, list); fileNames = list.toArray(new String[list.size()]); int index = seed.nextInt(fileNames.length); target = fileNames[index]; System.out.printf("target = %s at index = %d of %d files%n", target, index, fileNames.length); start = System.currentTimeMillis(); new Thread(forward).start(); new Thread(reverse).start(); } private static void collectFileNames(File folder, List<String> list) { folder.setReadOnly(); File[] files = folder.listFiles(); for(int j = 0; j < files.length; j++) { list.add(files[j].getName()); if(files[j].isDirectory()) collectFileNames(files[j], list); } } private static synchronized void post(int index, long end, String caller) { long time = end - start; System.out.printf("%s found %s at index %d in %d millis%n", caller, target, index, time); } private static Runnable forward = new Runnable() { public void run() { for(int j = 0; j < fileNames.length; j++) { if(fileNames[j].equals(target)) { long end = System.currentTimeMillis(); post(j, end, "forward"); break; } } } }; private static Runnable reverse = new Runnable() { public void run() { for(int j = fileNames.length-1; j >= 0; j--) { if(fileNames[j].equals(target)) { long end = System.currentTimeMillis(); post(j, end, "reverse"); break; } } } }; }
Similar Threads
-
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
How to get thread name
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:40 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM -
How come multi thread don't look like it?
By jkhoa in forum Threads and SynchronizationReplies: 1Last Post: 09-22-2007, 04:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks