Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads  RSS feed

 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, if in my main I hav:


I want to display the last part 'System.out.println(..)' when all Threads above have finished. Please help..

Just was able to: I may be able to check it with isAlive inside a while loop.. but is there a more efficient way?
[ November 09, 2007: Message edited by: Christian Long ]
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look into Thread.join()
 
Ugender Rekulampally
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian,
Look into Thread.join() method as suggested by Edward. But here is the solution for your problem.


join will make sure the thread is finished before it gets to the System.out.

Thanks,
Ugender
 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, Thanks..
, but now I have a problem:

I hav 2 producer (1 thread), 2 consumer(1 thread),1 class store.

in the store : methods,
// the store(that is, integer 'x') currently has 120 items
adding value y to the store (producer)
if store is full (x+y > 200)
norityAll();
wait();


retrieve value y from store (consumer)
notifyAll();

The problem I have is:
if a producer is adding a value y '110', it can't since the store would be full, so it would 'wait()'.
Then (it may happens that) maybe only '10' items would be retrieved .. and the consumer threads would end here.. (its ok)
However, the producer adding the value '110' would still be waiting (without ending) - what I would need is to check before 'waiting()' if consumer Threads are still alive. but how can I do something like that in these circumstances?

[ November 09, 2007: Message edited by: Christian Long ]
[ November 09, 2007: Message edited by: Christian Long ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand what you're trying to do, but to check if a thread is still alive, you can use the isAlive() method. However you should bear in mind that if isAlive() returns true, it's still possible that the thread dies just an instant after you made the call. So it's going to be hard to guarantee that a consumer thread is really still alive when you enter wait(). I would recommend you try to write the code in such a way that it doesn't depend on whether threads are still alive. Or try to guarantee that there will always be more threads available. It's hard to say how to do this though, because I don't really understand what you're doing.

One thing catches my eye here:

From what you said, you want to wait if the store is full. But after youwake up, you usually need to double check to see if the store is still full or not. It's possible for a wait() to complete prematurely, and for this reason (and others) you should always use wait() inside a loop that checks whether some condition is true or not. That condition represents the thing you are waiting for.

I also omitted the notifyAll() because there's usually not a good reason to call it immediately before or after a wait(). Generally there should be some other code somewhere which does a notify() or notifyAll() to wake up the waiting thread. Again though, I'm not sure I know what you're doing here; I'm just guessing.
[ November 09, 2007: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!