Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-20-2007, 05:33 AM
Member
 
Join Date: Aug 2007
Posts: 15
one198 is on a distinguished road
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 you

Last edited by one198 : 11-20-2007 at 06:14 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-21-2007, 09:01 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
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; } } } }; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
data from the main/GUI thread to another runnin thread... cornercuttin Threads and Synchronization 2 04-23-2008 11:30 PM
How to get thread name Java Tip java.lang 0 04-09-2008 07:40 PM
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 08:02 AM
Creating a Thread (extending Java Thread Class) JavaForums Java Blogs 0 12-19-2007 10:31 AM
How come multi thread don't look like it? jkhoa Threads and Synchronization 1 09-22-2007 05:25 AM


All times are GMT +3. The time now is 12:19 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org