Ciao,
Sto cercando di leggere da un Selector/ServerSocket contemporaneamente con 3 thread. Ho seguito questo esempio: http://www.javaworld.com/javaworld/j...ct.html?page=1 . La pagina dimostra codice funzionante per un singolo thread. Dai javadoc: Selectors are themselves safe for use by multiple concurrent threads; their key sets, however, are not.
Allora ho scritto il codice che si sincronizza sul selector per ottenere una lista di chiavi diversa per ogni thread:
Purtroppo non ottengo chiavi diverse, anzi tutti i thread cercano di agire sulle stesse chiavi. E' che il selector mi restituisce le stesse chiavi in chiamate successive finché non sono processate?codice:List keys = new LinkedList(); //lista di chiavi separata per ogni thread while (true) { keys.clear(); synchronized(selector) { int num = selector.select(); if (num == 0) continue; Set selectedKeys = selector.selectedKeys(); //mi aspettavo che questo producesse chiavi separate... keys.addAll(selectedKeys); selectedKeys.clear(); } Iterator it = keys.iterator(); while (it.hasNext()) { SelectionKey key = (SelectionKey) it.next(); if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) { Socket s = serverSocket.accept(); SocketChannel sc = s.getChannel(); sc.configureBlocking( false ); sc.register( selector, SelectionKey.OP_READ ); } else if ((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) { //.....

Rispondi quotando