multithreading - How do I signal a sleeping thread in Java? -


i'm writing udp client-server pair networks class, , have hit on problem. rather unorthodox networks assignment, little background first:

the goal create server implement push-based notifications. key point here server has contact client @ whatever address last seen, listen client's control packets. therefore, have thread running on client periodically sending out udp packets server, logs origin when needs send out response. technique busts through nat's, send refreshes address translation.

so then, here dilemma: unless i'm mistaken, nat maps own address , generated port number onto it's clients address port combination. therefore, in order traverse nat, need move packets through 1 port on client machine. updater thread have listen time, push out update packet, , go listening.

then here hairy. if original thread, wants perform action, wants port, has wake announcer, blocking while waiting response.

how can pull off in java?

p.s.: if turns out nat allow communication on different port go through, things awesome.

note: not telling right way solve larger problem.

but answer top-line question, "how signal sleeping thread in java" is: call interrupt() on thread. you'll need more elaborate mechanism in place communicate why has been interrupted, that's start. interrupt() wake sleep()ing or wait()ing thread interruptedexception, don't think that's you're asking.

this not wake thread blocked on read() call, socket. sounds using datagramsocket, in case have couple of options:

  • use non-blocking implementation. (aka, "selector-based", or new i/o (nio) in java lingo) see e.g. datagramchannel; maybe this question and/or this one
  • use normal java i/o, set socket timeout of suitable length, , wrap calls read() in loop, checking appropriate condition.

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -