~/ learn/ comp-348/ cards/ Concurrency for network programs
1 of 30

A resolver task as a Runnable

A resolver task as a Runnable

Answer

class Resolver implements Runnable { private final String host; Resolver(String host) { this.host = host; } @Override public void run() { try { System.out.println(host + " " + InetAddress.getByName(host).getHostAddress()); } catch (UnknownHostException ex) { System.err.println(host + " unresolved"); } } }

The task holds only what it needs (one hostname, final) and swallows nothing silently — it reports the failure on stderr. Because it implements `Runnable` rather than extending `Thread`, the same object can later be submitted to an executor untouched.

Harold 4e ch3 §Running Threads

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/c1522018-69e3-4cd0-84fd-8dbd48f458e5/flashcard utf-8 LF