• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads  RSS feed

 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I have an issue while programming in the Threads.I am using runtime class
to execute some commands in my java program.Since the output was hanging,I redirected the output to one stream and error to another using threads.Now my program is running.But now I have a situation where in my program I have to wait for the process to end,but in reality this process will be running.But I need an indication whether the process has started or not from either the error stream or output stream as the condition may be.How can I give an indication to the main prgram from either of these two threads.Can I wait on two thread at the same time and proceed if one gives back the notification
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you considered a Semaphore?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Won't that wait until both threads release? I think the requirement is for the main thread to proceed if either stream reader thread finds something in the output streams. Could you set the initial value to 1? I wonder how that would be different than a CountdownLatch at that point. Guess I need to study up on those.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to "Threads and Synchronization."
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Won't that wait until both threads release?


Run it and see
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, cute. I was thinking of the countdown latch when reading the semaphore in your code. See if I got it ... The main program says two threads can acquire this at once, starts two threads which do so, and then tries to acquire it again. As soon as one of the two threads releases, the main thread is able to acquire and goes on its way.

Thanks!
 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the jdk on which our current j2ee project is running is 1.4.2.I think that doesnt contain the concurrent package.As of now what I have done is to declare a new variable in my main thread which is public and static and made the threads to update this variable and check continuously for the value of this variable in my main thread until gets an appropriate value from one of the error or output threads.IS this a good programming practice?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Renu Radhika:
But the jdk on which our current j2ee project is running is 1.4.2.I think that doesnt contain the concurrent package.As of now what I have done is to declare a new variable in my main thread which is public and static and made the threads to update this variable and check continuously for the value of this variable in my main thread until gets an appropriate value from one of the error or output threads.IS this a good programming practice?


It sounds like what you are doing is spin locking, which is only
a good idea if the wait is very short. Since you are waiting for one of
two processes to terminate, I'm thinking that the asnwer to the question is "no".

The java.concurrent packages were written by Doug Lea, and if you can't
update to the current version of Java, I think you can find them as
a separate library. (I just googled on "Doug Lea concurrent util and this
was the first hit: http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html )

Alternately, you can just write a little code to solve this problem using
wait and notify: you are synchronzing on a variable that records, say, the
number of terminated processes. It is originally 0 and when the processes
in question terminate, they increment the counter. Your original thread
is waiting for the counter to become positive. It's not too hard: here's
a tutorial: http://java.sun.com/docs/books/tutorial/essential/threads/index.html
[ December 08, 2005: Message edited by: Jeff Albrechtsen ]
 
Scott Selikoff
author
Bartender
Posts: 4087
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albrechtsen:
Jeff: worst SCJP. Ever.


*completely off subject* but I've been wondering, how bad was it?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Selikoff:


*completely off subject* but I've been wondering, how bad was it?


The plot was predictable and no special effects worthy of the name.
Wait for it to come out on DVD.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!