Results 1 to 2 of 2

Thread: using Thread

  1. #1
    one198 is offline Member
    Join Date
    Aug 2007
    Rep Power

    Default 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 05:14 AM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    Java Code:
    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) {
            File[] files = folder.listFiles();
            for(int j = 0; j < files.length; j++) {
                    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");
        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");

Similar Threads

  1. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM
  2. How to get thread name
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:40 PM
  3. Replies: 0
    Last Post: 01-28-2008, 07:02 AM
  4. How come multi thread don't look like it?
    By jkhoa in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-22-2007, 04:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts