0

I'm kind of confused about this. I want to create two threads that works in the same data. For example if I have an array of Strings then I want this two threads to work in each unique item say add a letter 'p' in each strings. My question is where do I create this array of string is it outside/inside the Runnable class or create a new class with the array inside and the functions that access the contents of the array? I believe this also requires synchronization since I don't want the two threads to access the same data. Can someone give me an example? A pseudocode will do. Thanks in advance!

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by jazz_vill
0

I think I got it using the Producer Consumer Problem this is a little different i did not add 'p' on each items of the array but the important part here is that I was able to show the array using two threads though I only printed the current element of the array that the thread is working. :)

public class Example {
    String s[] = {"This","is","a","test!"};
    int c = 0;
    
    public static void main(String args[]){
        Example e= new Example();
        Thread thread1 = new Thread(e.new ItemPrinter("thread1"));
        Thread thread2 = new Thread(e.new ItemPrinter("thread2"));
        try {
            Thread.sleep(100);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        
        try{
            thread1.join();
            thread2.join();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
    
    class ItemPrinter implements Runnable {
        Thread myThread;
        ItemPrinter(String threadName){
            myThread = new Thread(this,threadName);
            myThread.start()
        }
        
        @Override
        public void run(){
            while(c < s.length) {
                synchronized(s){
                    System.out.println("Value of C: "+c);
                    String valueOfIndex = s[c];
                    printValue(valueOfIndex);
                    increment();
                    s.notifyAll();
                }
                try{
                    Thread.sleep(10);
                }catch(InterruptedException e){
                    
                }
            }
            

        }

        public synchronized void increment(){
            c++;
        }

            private void printValue(String valueOfIndex) {
                System.out.print(myThread.getName()+" got ");
                System.out.println("Index Value: "+valueOfIndex+"\n");
            }
        }
}

Sample Output:
Value of C: 0
thread1 got Index Value: This

Value of C: 1
thread2 got Index Value: is

Value of C: 2
thread1 got Index Value: a

Value of C: 3
thread2 got Index Value: test!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.