3
public class wshop8 {
    public static void main(String[] args) {
        SearchThread[] threads = new SearchThread[5];

        String[][] s = { 
                { "java", "I love java", "c++", "python" }, 
                { "c programs", "cookies", "java developers", "oops"}, 
                { "john", "doe", "mary", "mark holmes"}, 
                { "hello java java", "byebye", "java again?", "what?"}, 
                { "toronto", "montreal", "quebec city", "calgary"} 
            };

        for(int i=0;i<threads.length;i++){
            threads[i] = new SearchThread( s[i] );  //Insert each line of String s into threads array
            threads[i].start(); //Threads begin...
        }
        for(int i=0;i<threads.length;i++){
            try{
                threads[i].join();
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

class SearchThread extends Thread{
    private String[] search;

    public SearchThread(String[] search){this.search = search;}

    public synchronized void run(){
        for(int i=0;i<search.length;i++){
            System.out.print(search[i]+" ");
        }
        System.out.println();
    }
}

Hi. Above is the code I made at the moment..

Every time I compile the program, it gives me random sequence of outputs.

I think the threads are intercepting each other.

Any tips to make the program to output the string from s[0] to s[4] ?

I've also tried to set the priority but I failed.


Below is another version I've also tried. (not using the array of an obj thread) But also output randomly.

public class wshop8 {
    public static void main(String[] args) {
        SearchThread thread;

        String[][] s = { 
                { "java", "I love java", "c++", "python" }, 
                { "c programs", "cookies", "java developers", "oops"}, 
                { "john", "doe", "mary", "mark holmes"}, 
                { "hello java java", "byebye", "java again?", "what?"}, 
                { "toronto", "montreal", "quebec city", "calgary"} 
            };

        for(int i=0;i<5;i++){
            thread = new SearchThread( s[i] );  //Insert each line of String s into threads array
            thread.start(); //Threads begin...
        }
    }
}

class SearchThread extends Thread{
    private String[] search;

    public SearchThread(String[] search){this.search = search;}

    //override
    public synchronized void run(){

        for(int i=0;i<search.length;i++){
            System.out.print(search[i]+" ");
        }
        System.out.println();
    }
}
Jungmin
  • 41
  • 4
  • 6
    That's the whole idea of parallelism. If you want sequential processing, the easiest way is to use a single thread. – Kayaman Jul 10 '14 at 14:05
  • 2
    Was your intention to concurrently process the strings in each batch, but run the batches in sequence? – Dave Morrissey Jul 10 '14 at 14:08

1 Answers1

1

There are two solutions for this problem , first one is simply use one thread so that you can output the string from s[0] to s[4] . second one is to add a static Object to the SearchThread class

private static Object objectUsedForSynchronization=new Object();

then inside the run method synchronize the content on the objectUsedForSynchronization :

 public  void run(){
  synchronize(objectUsedForSynchronization){
        for(int i=0;i<search.length;i++){
            System.out.print(search[i]+" ");
        }
        System.out.println();
    }
}
Mifmif
  • 3,132
  • 18
  • 23