1

I need a little help with java multithread. I have this class:

public class EdgeServer{

    private static final int ServidorBordaID = 9;
    private static final String urlLogin = "http://localhost/exehdager-teste/index.php/ci_login/logar";
    private static final String insertSensorURI = "http://localhost/exehdager-teste/index.php/cadastros/ci_sensor/gravaSensor";
    private static final String insertGatewayURI = "http://localhost/exehdager-teste/index.php/cadastros/ci_gateway/gravaGateway";
    private static ArrayList<Gateway> gatewaysCadastrados = new ArrayList<>();

    public static void main(String[] args) {
        // Start a user thread that runs the UPnP stack
        Thread clientThread = new Thread(new Descoberta());
        clientThread.setDaemon(false);
        clientThread.start();

        Thread publicationThread = new Thread(new Publication());
        publicationThread.setDaemon(false);
        publicationThread.start();
    }
}

The thread Descoberta will add new itens to gatewaysCadastrados list on demand. and the Publication thread will read this list and execute an action for each object on list.

I just need to know how to share and pass this var to threads. Will I need to build a semaphore to do this?

Hubertokf
  • 308
  • 1
  • 4
  • 12
  • Some good information on collections here: http://java-performance.info/java-collections-overview/ - Includes a description of the available multithreaded collections. – SlopeOak Oct 23 '15 at 11:45
  • Pass the objects to the runnables. Here is a similar question: http://stackoverflow.com/questions/17756255/accessing-a-variable-of-a-thread-from-another-thread-in-java – Robert Reiner Oct 23 '15 at 11:46

2 Answers2

1

Here is the sample code where you can share list between two threads and you need to use wait and notify for semaphore.

public class Descoberta extends Thread {
private  final ArrayList<Gateway> a = new ArrayList<>();
public Descoberta( ArrayList<Gateway> a) {
        this.a = a;
    }

    @Override
    public void run() {
         synchronized (a) {
                while(true){ // your condition
                    a.wait();
                }
                a.notify();
         }
    }
}

public class Publication extends Thread {
private  final ArrayList<Gateway> b = new ArrayList<>();
public Publication(ArrayList<Gateway> b) {
        this.b = b;
    }

    @Override
    public void run() {
         synchronized (b) {
                while(true){ // your condition
                    b.wait();
                }
                b.notify();
         }
    }
}

public class EdgeServer {
    public static void main(String args[]) {
        private final ArrayList<Gateway> gatewaysCadastrados = new ArrayList<>();
        Thread clientThread = new Descoberta(gatewaysCadastrados);
        Thread publicationThread = new Publication(gatewaysCadastrados);
        clientThread.start();
        publicationThread.start();
    }
}
2787184
  • 3,749
  • 10
  • 47
  • 81
  • But those threads will share the same data? I mean, if thread clientThread insert a item on gatewaysCadastrados, publicationThread will see it? And i really will need a semaphore assuming that just one thread will modify the list? – Hubertokf Oct 23 '15 at 11:55
  • @Hubertokf have a look at for example "CopyOnWriteList". – Fildor Oct 23 '15 at 12:14
  • Sorry, [CopyOnWriteArrayList](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html) – Fildor Oct 23 '15 at 12:30
  • @Fildor yeah! I Made a copy of the list using that.. Thanks very much! – Hubertokf Oct 23 '15 at 12:48
0

A simple way it to pass the shared object as a constructor parameter to the relevant runnables; e.g.

Thread clientThread = new Thread(new Descoberta(gateways));
...
Thread publicationThread = new Thread(new Publication(gateways));

Obviously, the respective Runnable constructors need to save the parameters so that their run() methods can find them.

There are various other ways:

  • If the runnables are inner classes within the same outer class instance, they can access shared objects in outer class.

  • If the shared state is stored in static variables, then the runnables can access them via static getters, etcetera. (NOTE: this is most likely bad design ...)


And as @Fidor points out, if two or more threads are going to share a common (mutable) data structure, then they need to synchronize their read and write operations on the data structure. If you neglect this, your application is liable to have the kind of insidious bugs that are hard to reproduce and hard to track down.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • @Hubertokf If you share a resource this way, you need to consider synchronization of that resource. The "concurrent" namespace has some datastructures already doing this for you. – Fildor Oct 23 '15 at 11:50