~/ learn/ comp-348/ cards/ Why network code is concurrent, and how to start a thread
1 of 4

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/87802cbf-ef29-4cc7-adf5-2b2143f80941/flashcard utf-8 LF