java - How to make sure the main thread continues only after child thread has started? -


i trying write program first ssh'es 2 different machines, , executes http requests against them. means in order able execute http requests ssh tunnel should running.

what have done have 2 threads, each running ssh command 1 of boxes:

    thread thread1 = new thread(new runnable(){             public void run(){                 try{                     process p1 = runtime.getruntime().exec("ssh -a -l12345:localhost:54321 firsthost.com");                     p1.waitfor();                 }catch (exception e){}             }         }) ;         thread1.start();      thread thread2 = new thread(new runnable(){         public void run(){             try{                 process p2 = runtime.getruntime().exec("ssh -a -l12345:localhost:54321 secondhost.com");                 p2.waitfor();             }catch (exception e){}         }     }) ;     thread2.start(); 

now problem after starting threads, not start running, means send requests before connection made. there simple way (without using locks or mutex) can make sure return main program after threads started? (i don't want of course wait them end because never end. run first command once :) if there better way run processes in background instead of having these 2 seperate threads, great too!)

thanks!

try adding -f , -n flags tell ssh background once port forwarding setup. can execute ssh commands in main thread , wait until exit (i.e. background themselves) before continuing.

-f

requests ssh go background before command execution. useful if ssh going ask passwords or passphrases, user wants in background. implies -n. recommended way start x11 programs @ remote site ssh -f host xterm.

if exitonforwardfailure configuration option set “yes”, client started -f wait remote port forwards established before placing in background.

-n

do not execute remote command. useful forwarding ports (protocol version 2 only).

(unfortunately i'm unable test @ moment. apologies if doesn't work.)


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -