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!

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 {
        } catch (InterruptedException e1) {
        }catch(InterruptedException e){
    class ItemPrinter implements Runnable {
        Thread myThread;
        ItemPrinter(String threadName){
            myThread = new Thread(this,threadName);
        public void run(){
            while(c < s.length) {
                    System.out.println("Value of C: "+c);
                    String valueOfIndex = s[c];
                }catch(InterruptedException e){


        public synchronized void increment(){

            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 article has been dead for over six months. Start a new discussion instead.