Posted By:
Anonymous
Posted On:
Thursday, September 20, 2001 06:06 AM
I read the nio tutorial in www.javaworld.com and magazine. I also read the API. I feel that using NIO is very hard because it's not normal procedure to do IO opreation for me. So, I made the MyPing after I read Ping Example. Please examine my code and teach me a lesson. When All ping operation is done, my connect thread is blocked because connect thread is waiting to get monitor, so I used some trick to quit. I doubt whether I made this program correctly. MyPing works correctly but I'm not sure that my program is reliable. Here's the code import java.util.*; import java.net.*; import java.nio.*; import java.nio.channels.*; import java.io.*;
More>>
I read the nio tutorial in www.javaworld.com and magazine. I also read the API. I feel that using NIO is very hard because it's not normal procedure to do IO opreation for me. So, I made the MyPing after I read Ping Example. Please examine my code and teach me a lesson. When All ping operation is done, my connect thread is blocked because connect thread is waiting to get monitor, so I used some trick to quit. I doubt whether I made this program correctly. MyPing works correctly but I'm not sure that my program is reliable.
Here's the code
import java.util.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.io.*;
public class MyPing
{
public static void main(String[] args) throws Exception
{
Target t1 = new Target(args[0]);
Target t2 = new Target(args[1]);
Connector connector = new Connector();
connector.start();
connector.add(t1);
connector.add(t2);
connector.join(500);
connector.shutdown();
}
}
class Target {
private InetSocketAddress netAddress;
private boolean isOpen;
public Target(String target_addr) {
netAddress = new InetSocketAddress(target_addr, 80);
}
public InetSocketAddress getNetAddress() {
return netAddress;
}
public void setIsOpen(boolean result) {
isOpen = result;
}
public boolean getIsOpen() {
return isOpen;
}
}
class Connector extends Thread {
private LinkedList targetList;
private Selector trySelect;
private Printer printerForNotification;
private boolean shutdown;
public Connector() throws IOException {
setName("Connector");
targetList = new LinkedList();
trySelect = Selector.open();
printerForNotification = new Printer(trySelect);
printerForNotification.start();
shutdown = false;
}
public void add(Target t) {
synchronized(targetList) {
targetList.add(t);
targetList.notifyAll();
}
}
public void shutdown() {
shutdown = true;
synchronized(targetList) {
targetList.notifyAll();
}
}
public void run() {
while(shutdown == false) {
try {
Target target;
synchronized(targetList) {
if (targetList.size() == 0) {
targetList.wait();
}
if(shutdown) return;
target = (Target) targetList.removeFirst();
}
SocketChannel try_channel = SocketChannel.open();
try_channel.configureBlocking(false);
try_channel.register(trySelect, SelectionKey.OP_CONNECT);
try_channel.connect(target.getNetAddress());
trySelect.wakeup();
}
catch(InterruptedException ie) {}
catch(IOException ioe) {}
}
}
}
class Printer extends Thread
{
private Selector selectForObservation;
public Printer(Selector target_select) {
setName("Printer");
setDaemon(true);
selectForObservation = target_select;
}
public void run() {
while(true) {
try {
selectForObservation.selectNow();
Set readyKeys = selectForObservation.selectedKeys();
if (readyKeys.size() == 0) continue;
for(Iterator i = readyKeys.iterator(); i.hasNext(); ) {
SelectionKey key = (SelectionKey)i.next();
i.remove();
SocketChannel connected_channel = (SocketChannel)key.channel();
System.out.println(connected_channel.socket().toString());
connected_channel.close();
}
}
catch(IOException ioe) {}
catch(ClosedSelectorException cse) {}
}
}
}
Please use html tags to format code blocks.
<<Less