0

Hi i have made a something that extends thread that adds adds an object that has a IP in it. then i made two instances of this thread and started them. they use the same list.

I now want to use Synchronized to stop the concurrent update problem. But its not working and i cant work out why.

My main class:

import java.util.*;
import java.io.*;
import java.net.*;

class ListTest2 {
    public static LinkedList<Peer>  myList = new LinkedList<Peer>();

    public static void main(String [] args) {   
        try {
            AddIp test1 = new AddIp(myList);
            AddIp test2 = new AddIp(myList);

            test1.start();
            test2.start();      
        } catch(Exception e) {
            System.out.println("not working");
        }
    }
}

My thread class:

 class AddIp extends Thread {
     public static int startIp = 0;

     List<Peer> myList;

     public  AddIp(List<Peer> l) {
         myList = l;
     }


     public synchronized void run() {      
        try {
            startIp = startIp+50;
            int ip = startIp;
            InetAddress address = InetAddress.getByName("127.0.0.0");
            Peer peer = new Peer(address);

            while(ip <startIp+50) { 
                ip++;
                address = InetAddress.getByName("127.0.0."+ip);

                peer = new Peer(address);

                myList.add(peer);

                if(myList.indexOf(peer)== (myList.size() -1)) {
                } else {
                    System.out.println("Lost"+peer.peerIp);
                }
            }     
        } catch(Exception e) {
        }
    }
}

Can anyone help me out here im lost for ideas thanks.

4 Answers 4

5
 public synchronized void run() 

Synchronizes on calling instance: this.

So, 1st thread synchronizes on test1 and 2nd thread synchronizes on test2, which doesn't help at all.

You want to synchronize on the shared resource, in this case: myList

public void run() {
  synchronize(myList){
   //your Logic
  }
}

As a side note: Implement runnable instead of extending a Thread. Read more here.

Sign up to request clarification or add additional context in comments.

2 Comments

i have tried this and im getting compiler error ";" expected. any ideas why?
all good worked it out synchronized not synchronize and it worked. thanks for the help.
1

You'd be better off implementing Runnable oppose to extending thread

also

public void run() {
  synchronize(list){
   //stuffs
  }
}

Comments

0

they use the same list.

You can try to use Vector instead List. Vector is synchronized

or set your List to be synchronized:

List myList = Collections.synchronizedList(myList);

instead to use:

synchronize(myList){

 } 

Comments

0

The easiest way is to use a List implementation that can handle multiple threads. Try CopyOnWriteArrayList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.